我是 RSpec 的新手,我不知道如何测试以下内容:
在我的应用程序 Controller 中(在 Rails 3 应用程序中),我在一个 before 过滤器中设置了语言环境,就像这样
def set_locale
I18n.locale = ["en", Setting.locale, get_locale_from_subdomain].compact.last
end
def get_locale_from_subdomain
locale = request.subdomain.split('.').first
return nil unless LOCALES.include? locale
locale
end
所以基本上,'en.example.com' 和 'example.com' 将有一个“en”语言环境,而 'fr.example.com' 会将语言环境设置为“fr”。
我该如何测试?
最佳答案
我会做这样的事情:
I18n.available_locales.each do |locale|
request.host = "#{locale}.example.com"
get :index
I18n.locale.should == locale
end
使用这里描述的匿名 Controller :
https://www.relishapp.com/rspec/rspec-rails/v/2-4/docs/controller-specs/anonymous-controller
另请参阅此问题:
Rails rspec set subdomain
关于ruby-on-rails - 使用 RSpec : setting locale based on first subdomain 进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5710904/