Rails i18n locale中间件测试

时间:2018-06-17 23:33:24

标签: ruby-on-rails internationalization locale middleware minitest

我正在实施一个Rails 5.2应用程序,并使用Ryan Tomayko的区域设置中间件https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/locale.rb通过浏览器首选项设置区域设置,并且工作正常。

现在我正在尝试为此功能实施测试(使用minitest)。我把测试写成如下:

allow delete: if request.auth.uid != null

第一次测试通过。但是在第二次测试中,实际结果仍然使用英语语言环境。因此,测试似乎没有运行中间件。

我找到了以下帖子Making Rails tests aware of Rack middleware outside Rails's internal chain,其中接受的答案表明我应该将以下内容添加到test_helper.rb

require 'test_helper'

class LocaleTest < ActionDispatch::IntegrationTest
  test 'use en locale' do
    get root_path, headers: { 'Accept-Language': 'en' }
    I18n.locale = :en
    assert_select 'a', t('views.defaults.app_title')
  end

  test 'use pt locale' do
    get root_path, headers: { 'Accept-Language': 'pt' }
    I18n.locale = :pt
    assert_select 'a', t('views.defaults.app_title')
  end
end

但是当我这样做时,我收到以下错误

ActionDispatch::IntegrationTest.app = Rack::Builder.new do
  eval File.read(Rails.root.join('config.ru'))
end

config.ru文件是rails 5.2

附带的默认文件
rake aborted!
TypeError: no implicit conversion of nil into String
/home/m/projetos/fleetman/test/test_helper.rb:27:in `eval'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/realpath_cache.rb:17:in `realpath'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/realpath_cache.rb:7:in `block in initialize'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/realpath_cache.rb:11:in `call'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:41:in `require_relative'
(eval):3:in `block in <main>'
/home/m/projetos/fleetman/test/test_helper.rb:27:in `eval'
/home/m/projetos/fleetman/test/test_helper.rb:27:in `block in <main>'
/home/m/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/builder.rb:55:in `instance_eval'
/home/m/.rvm/gems/ruby-2.5.1/gems/rack-2.0.5/lib/rack/builder.rb:55:in `initialize'
/home/m/projetos/fleetman/test/test_helper.rb:26:in `new'
/home/m/projetos/fleetman/test/test_helper.rb:26:in `<main>'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/home/m/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
/home/m/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
/home/m/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require'
/home/m/projetos/fleetman/test/controllers/account_activations_controller_test.rb:1:in `<main>'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/home/m/.rvm/gems/ruby-2.5.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/home/m/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
/home/m/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
/home/m/.rvm/gems/ruby-2.5.1/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require'
/home/m/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/test_unit/runner.rb:50:in `block in load_tests'
/home/m/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/test_unit/runner.rb:50:in `each'
/home/m/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/test_unit/runner.rb:50:in `load_tests'
/home/m/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/test_unit/runner.rb:39:in `run'
/home/m/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/test_unit/runner.rb:35:in `rake_run'
/home/m/.rvm/gems/ruby-2.5.1/gems/railties-5.2.0/lib/rails/test_unit/testing.rake:16:in `block in <main>'
/home/m/.rvm/gems/ruby-2.5.1/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/home/m/.rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:15:in `eval'
/home/m/.rvm/gems/ruby-2.5.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test
(See full trace by running task with --trace)

如何实现区域设置中间件的测试?

0 个答案:

没有答案