Testing with RSpec: locale setup based on the first subdomain

I am new to RSpec and I cannot learn how to test the following:

In my application controller (in a Rails 3 application) I set the locale before the filter, e.g.

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 “en” locale, while “fr.example.com” will set the locale to “fr".

How can I check this?

+5
source share
1 answer

I would 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

. :

rspec

+4

All Articles