在config.routes.rb文件中:
scope '(:locale)' do
resources :techniques, path: '/applications' do
get '/complete_list' => 'techniques#complete_list'
end
end在我的Gemfile
group :development, :test do
gem 'rspec-rails'
gem 'byebug'
gem 'better_errors'
gem 'factory_girl_rails'
gem 'faker'
end
group :test do
gem 'poltergeist'
gem 'capybara'
gem 'launchy'
gem 'database_cleaner'
end在我的application_controller.rb
before_filter :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options(options = {})
{ locale: I18n.locale }.merge options
end在我的规范中:
visit techniques_path它总是不及格:
I18n::InvalidLocale - "applications" is not a valid locale:它在我的application_controller中突出显示了这一行:
I18n.locale = params[:locale] || I18n.default_locale我可以通过将规范更改为以下内容来使其正常工作:
visit techniques_path(locale: :en)但我认为在应用程序控制器中设置default_url_options会自动处理这些问题。这里我漏掉了什么?
发布于 2015-08-18 22:36:17
当你想测试来自ApplicationController的行为时,你需要一个所谓的匿名控制器,一个从ApplicationController继承并且可测试的控制器:
describe ApplicationController do
controller do
def index
end
end
describe "language setting" do
it "uses parameter" do
expect(I18n).to receive(:locale=).with('en')
get :index, locale: 'en'
end
it "falls back to default_locale" do
I18n.default_locale = 'nl'
expect(I18n).to receive(:locale=).with('nl')
get :index
end
end
end编辑:我现在看到您需要将locales参数添加到feature-test中。
当您想要将参数传递到路径中时,只需将它们作为散列添加:
visit techniques_path({locale: 'en'})然而,我发现在特性测试中使用url_helpers是一种糟糕的做法。我假设“am”是功能/集成测试,因为我还没有在其他地方看到过它。相反,在测试纯集成时,请使用实际字符串作为路径:
visit '/en/techniques/1234'
visit "/en/techniques/@technique.id"这个a.o.传达功能测试是一个单独的应用程序:一个不依赖于应用程序内部状态的应用程序。就像使用浏览器点击应用程序的“用户”一样。使用火狐浏览器的用户不能使用"technique_path",他只能点击链接,或者在浏览器栏中输入网址。
https://stackoverflow.com/questions/32073112
复制相似问题