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={})
{
Looking through how the controller test case generates the url there doesn't seem to be a direct way to have it use the defualt_url_options. The main block that actually does the url creationg (in the tests) looks like this (http://github.com/rails/rails/blob/master/actionpack/lib/action_controller/test_case.rb):
private
def build_request_uri(action, parameters)
unless @request.env['REQUEST_URI']
options = @controller.__send__(:rewrite_options, parameters)
options.update(:only_path => true, :action => action)
url = ActionController::UrlRewriter.new(@request, parameters)
@request.request_uri = url.rewrite(options)
end
end
This gets called by the process method which is in turn called by the get, post, head, or put methods. One way to maybe get what you are looking for might be to alias_chain the process method.
class ActionController::TestCase
def process_with_default_locale(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
parameters = {:locale=>'en'}.merge(parameters||{})
process_without_default_locale(action, parameters, session, flash, http_method)
end
alias_method_chain :process, :default_locale
end
You'll want to put that into your test helper, outside of the TestCase class I think. Let me know how it works for you, I haven't really tested it out so we'll see.
I've come up with a bit less invasive solution to this problem.
setup :set_default_locale
def set_default_locale
def @request.query_parameters
{:locale => "en"}.merge(@query_parameters)
end
end
The advantage of this solution is that it means you can only apply the default locale parameter to certain test cases, so you can test, for example, the redirection strategy itself.
Things changed again with Ruby 5.1. Here's what worked for me:
class ActionController::TestCase
module Behavior
module LocaleParameter
def process(action, params: {}, **args)
# Locale parameter must be a string
params[:locale] = I18n.locale.to_s
super(action, params: params, **args)
end
end
end
prepend Behavior::LocaleParameter
end
With integration testing, I found that the above monkey patch needs to be different, this is what worked for me (Rails 2.3.4):
class ActionController::Integration::Session
def url_for_with_default_locale(options)
options = { :locale => 'en' }.merge(options)
url_for_without_default_locale(options)
end
alias_method_chain :url_for, :default_locale
end