[rspec-users] Controller specs and default_url_options

Martin C.
Thu, 19 Jan 2012 16:45:00 -0000

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
(http://stackoverflow.com/questions/1987354/how-to-set-locale-default-url-options-for-functional-tests-rails/8920258#8920258
), 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,
http_method)
    end
    alias_method_chain :process, :default_locale
  end
end


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)
  end
  alias_method_chain :assert_recognizes, :default_locale
end


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,

Martin

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