In my application_controller, I have the following set to include the locale with all paths generated by url_for:
def default_url_options(options={})
{
In the Rails 3.1-stable branch, the process method is now within a Behavior
module. So here is the code which worked for me (slightly different from John Duff's answer):
class ActionController::TestCase
module Behavior
def process_with_default_locale(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
parameters = { :locale => I18n.default_locale }.merge( parameters || {} )
process_without_default_locale(action, parameters, session, flash, http_method)
end
alias_method_chain :process, :default_locale
end
end
And I made sure this code gets called before running the specs/tests. A good place to put it is in the test_helper class.
I tried a lot of examples, but only this one helped me. It is concise and simple. Add this code snippet to the test.rb:
Rails.application.configure do
# ... other config ...
routes.default_url_options[:locale] = :en
end
Works on Rails 5.1.4
alias_method_chain
is deprecated in Rails 5, and it seems the Behavior process method has changed.
Here's my modification of Martin Carel's answer above, adapted to Rails 5.
RSpec.configure do |config|
module ActionController
class TestCase
module Behavior
module LocaleParameter
def process(action, parameters = {params: {}})
unless I18n.locale.nil?
parameters[:params][:locale] = I18n.locale
end
super(action, parameters)
end
end
prepend Behavior::LocaleParameter
end
end
end
end
I'm by no means an expert in Rails or Ruby, so if something can be improved in this answer, let me know and I'll change it.
For Rails 5, I found this simple solution
In test_helper.rb
based on action_dispatch/testing/integration.rb
module ActionDispatch::Integration
class Session
def default_url_options
{ locale: I18n.locale }
end
end
end
In case anyone is using this with Rails 4.0, the order of arguments in the process method has changed, so you'll need to use this updated patch (based on @martin-carel's answer, just with the http_method
argument moved to the second argument):
class ActionController::TestCase
module Behavior
def process_with_default_locale(action, http_method = 'GET', parameters = nil, session = nil, flash = nil)
parameters = { :locale => I18n.locale }.merge( parameters || {} ) unless I18n.locale.nil?
process_without_default_locale(action, http_method, parameters, session, flash)
end
alias_method_chain :process, :default_locale
end
end
Hope that helps anyone stuck on this problem.
I ran into this problem with a failing cucumber test. I use locales as parameters in the url, i.e. http://mysite.com/home?locale=he
What I do to cope with this is to drop all locale related stuff from the url during testing by defining default_url_options depending on the environment I use:
# app/controllers/application_controller.rb
def default_url_options(options={})
logger.debug "default_url_options is passed options: #{options.inspect}\n"
ENV["RAILS_ENV"] != "cucumber" ? { :locale => I18n.locale } : {}
end