5

我是 RSpec 的新手,我不知道如何测试以下内容:

在我的应用程序控制器(在 Rails 3 应用程序中)中,我在前置过滤器中设置区域设置,如下所示

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”。

我该如何测试呢?

4

1 回答 1

4

我会做这样的事情:

I18n.available_locales.each do |locale|
  request.host = "#{locale}.example.com"
  get :index
  I18n.locale.should == locale
end

使用此处描述的匿名控制器:

https://www.relishapp.com/rspec/rspec-rails/v/2-4/docs/controller-specs/anonymous-controller

另请参阅此问题:

Rails rspec 设置子域

于 2011-09-15T19:40:07.033 回答