[rspec-users] Controller specs and default_url_options

Martin C. lists at ruby-forum.com
Thu Jan 19 11:45:00 EST 2012

Hi Jeremie,

I'm also using Rails 3.1.3 and rspec-rails 2.8.1 and I was in the same
situation as you: neither the rspec issue #255 or the proposed solutions
in this email thread solved my problems. But now I have something
working for me (and hopefully it will work for you too).

As I recently posted on Stackoverflow
), here is the code I use to inject the locale in the controller's
params (which makes all my controller specs pass without explicitly
specifying the locale in each controller spec):

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,
    alias_method_chain :process, :default_locale

And here is the "cheat" I use to inject the locale in Rails's
assert_recognizes method (which makes all my routing specs pass without
explicitly specifying the locale in each routing spec):

module ActionDispatch::Assertions::RoutingAssertions
  def assert_recognizes_with_default_locale(expected_options, path,
extras={}, message=nil)
    expected_options = { :locale => I18n.default_locale.to_s
}.merge(expected_options || {} )
    assert_recognizes_without_default_locale(expected_options, path,
extras, message)
  alias_method_chain :assert_recognizes, :default_locale

Stucking those two code snippets in my spec helper class means that I do
not need to change all my previous controller/routing specs during the
implementation of supporting multiple locales for my app.

Hope this helps,


Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list