ruby on rails - Controller spec and default_url_options -
i'm using rails 3.1.3 rspec-rails 2.8.1. have scope ':locale' in routes.rb , want run controller , routing specs. i'm aware of problem setting default_url_options in application.rb controller apply solution found in last comment on rspec issue #255 ( https://github.com/rspec/rspec-rails/issues/255 )
#./spec/support/default_locale.rb class actionview::testcase::testcontroller def default_url_options(options={}) { :locale => i18n.default_locale } end end class actiondispatch::routing::routeset def default_url_options(options={}) { :locale => i18n.default_locale } end end #./spec/controllers/categories_controller_spec.rb require "spec_helper" describe categoriescontroller describe "get index" "assigns categories @categories" category = factory :category :index assigns(:categories).to_a.should eq([category]) end end end
this test fails routing error if use "get :index, locale: :fr" instead of "get :index" test pass. test example of controller spec have failing tests routing , request. (i have no view specs i'm pretty sure fail)
i can't figure out problem come , why patch doesn't solve it. there thing ? (i put code in ./spec/support/default_locale.rb , verify loads correctly).
thanks in advance.
this code injects locale in controller's params (which makes controller specs pass without explicitly specifying 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 "cheat" use inject locale in rails's assert_recognizes method (which makes routing specs pass without explicitly specifying 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 2 code snippets in spec helper class means not need change previous controller/routing specs during implementation of supporting multiple locales app.
hope helps,
Comments
Post a Comment