Ruby on rails 使用RSpec进行测试:基于第一个子域设置语言环境

Ruby on rails 使用RSpec进行测试:基于第一个子域设置语言环境,ruby-on-rails,ruby-on-rails-3,rspec,rspec2,rspec-rails,Ruby On Rails,Ruby On Rails 3,Rspec,Rspec2,Rspec Rails,我是RSpec的新手,不知道如何测试以下内容: 在我的应用程序控制器(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

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

在我的应用程序控制器(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
使用如下所述的匿名控制器:

另见这个问题: