开发者

Testing with RSpec: setting locale based on first subdomain

I'm new to RSpec, and I can't find out how to test the following:

In my application controller (in a Rails 3 app), I set the locale in a before filter, like so

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

So basically, 'en.example.com开发者_JAVA百科' and 'example.com' will have an "en" locale, whereas 'fr.example.com' will set the locale to "fr".

How can I test this?


I'd do something like this:

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

With an anonymous controller as described here:

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

Also see this question:

Rails rspec set subdomain

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜