Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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' and 'example.com' will have an "en" locale, whereas 'fr.example.com' will set the locale to "fr".

How can I test this?

like image 579
David Sulc Avatar asked Oct 12 '22 06:10

David Sulc


1 Answers

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

like image 61
Jared Avatar answered Oct 31 '22 20:10

Jared