# Controller specs and default\_url\_options

**URL:** <https://www.ruby-forum.com/t/controller-specs-and-default-url-options/215821>\
**Category:** RSpec\
**Created:** [January 17, 2012, 12:33am UTC](https://www.ruby-forum.com/t/controller-specs-and-default-url-options/215821 "2012-01-17T00:33:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 17, 2012, 12:33am UTC](https://www.ruby-forum.com/t/controller-specs-and-default-url-options/215821/1 "2012-01-17T00:33:36Z")

</div>

Hello,

I’m using Rails 3.1.3 with rspec-rails 2.8.1. I have a scope ‘:locale’  
in routes.rb and I want to run controller and routing  
specs. I’m aware of the problem with setting default\_url\_options in  
application.rb controller so I apply the solution found in the last  
comment on rspec issue #255 (

> <https://github.com/rspec/rspec-rails/issues/255>
>
> Using Rails 3, I have a \`:scope\` in routes.rb:
> 
> \`\`\` Ruby
> scope "(/:locale)" do
> 
> … resources :products
> 
> end
> \`\`\`
> 
> In application\_controller.rb I set the default :locale
> 
> \`\`\` Ruby
> def default\_url\_options(options={})
> 
> {:locale =\> "en-US"}
> 
> end
> \`\`\`
> 
> In my views, this works when I test in my browser, but fails with Rspec:
> 
> \`\`\` Ruby
> \<%= link\_to product.name, product %\>
> \`\`\`
> 
> While running specs I see:
> 
> \`\`\`
> No route matches {:action=\>"show", :controller=\>"products", :locale=\>#&lt;Product id: ...&gt;}
> \`\`\`
> 
> So, Rspec is not using the \`default\_url\_options\` set in application\_controller.rb.
> 
> Thanks

)

#./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 do

describe “GET index” do  
it “assigns all categories as @categories” do  
category = Factory :category

```
  get :index
  assigns(:categories).to_a.should eq([category])
end

```

end  
end

This test fails with routing error but if I use “get :index,  
locale: :fr” instead of just “get :index” the test pass.  
This test is a example of controller spec but I have failing tests for  
routing and request. (I have no view specs but  
I’m pretty sure they would also fail)

I can’t figure out where the problem come from and why the patch  
doesn’t solve it. Is there another thing to do ? (I just put the code  
in ./spec/support/default\_locale.rb and verify that it loads  
correctly).

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![titinux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/838e76/32.png) [@titinux](https://www.ruby-forum.com/u/titinux)\
**Post date:** [January 17, 2012, 3:16am UTC](https://www.ruby-forum.com/t/controller-specs-and-default-url-options/215821/2 "2012-01-17T03:16:12Z")

</div>

On Jan 16, 2012, at 5:17 PM, Titinux wrote:

> class ActionView::TestCase::TestController
> 
> ```
> assigns(:categories).to_a.should eq([category])
> 
> ```
> 
> I can’t figure out where the problem come from and why the patch  
> doesn’t solve it. Is there another thing to do ? (I just put the code  
> in ./spec/support/default\_locale.rb and verify that it loads  
> correctly).
> 
> Thanks in advance.

No guarantees here, but it’s possible that  
ActionView::TestCase::TestController is not loaded yet, in which case  
its own definition of default\_url\_options would clobber yours when it  
does get loaded. Try this instead:

ActionView::TestCase::TestController.class\_eval do  
undef\_method :default\_url\_options  
def default\_url\_options(options={})  
{ :locale =\> I18n.default\_locale }  
end  
end

ActionDispatch::Routing::RouteSet.class\_eval do  
undef\_method :default\_url\_options  
def default\_url\_options(options={})  
{ :locale =\> I18n.default\_locale }  
end  
end

If those classes aren’t loaded yet, Rails will find and load them first  
(via its autoload strategy) before invoking class\_eval on them, thus  
ensuring that you’re replacing the existing methods rather than writing  
methods that will later be replaced.

HTH,  
David

---

<div class="post-metadata">

**Author:** ![titinux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/838e76/32.png) [@titinux](https://www.ruby-forum.com/u/titinux)\
**Post date:** [January 17, 2012, 5:12pm UTC](https://www.ruby-forum.com/t/controller-specs-and-default-url-options/215821/3 "2012-01-17T17:12:25Z")

</div>

Hi,

I used your code but it does not work either.

To eliminate the possibility of a problem coming from my application I  
made a new Rails 3.1.3 app from scratch and scaffold Category stuff.

And the result is that view specs start working with either issue #255  
solution or yours but controller, routing and request specs fails  
whatever I try.

I’ve set a “binding.pry” just before “get :index” in the controller  
spec to have a better vision of what’s going on

ActionView::TestCase::TestController.new.default\_url\_options =\>  
{:locale=\>:en}  
ActionDispatch::Routing::RouteSet.new.default\_url\_options =\>  
{:locale=\>:en}

So I think the patch does its job but “get :index” takes another path  
and not use default\_url\_options from  
ActionView::TestCase::TestController nor  
ActionDispatch::Routing::RouteSet

---

<div class="post-metadata">

**Author:** ![titinux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/838e76/32.png) [@titinux](https://www.ruby-forum.com/u/titinux)\
**Post date:** [January 19, 2012, 5:45pm UTC](https://www.ruby-forum.com/t/controller-specs-and-default-url-options/215821/4 "2012-01-19T17:45:00Z")

</div>

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  
([testing - How to set locale default\_url\_options for functional tests (Rails) - Stack Overflow](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

---

<div class="post-metadata">

**Author:** ![titinux](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/838e76/32.png) [@titinux](https://www.ruby-forum.com/u/titinux)\
**Post date:** [November 12, 2012, 4:07pm UTC](https://www.ruby-forum.com/t/controller-specs-and-default-url-options/215821/5 "2012-11-12T16:07:40Z")

</div>

Thanks for these very useful informations.

I found the titinux’ first post to solve my problems with request specs,  
and martin\_c94’s post to solve my controller specs problems.

Still, in the console, the url helpers need the locale explicitly set,  
it seems:

[1] pry(main)\> app.user\_path User.first  
ActionController::RoutingError: No route matches {:action=\>“show”,  
:controller=\>“users”, :locale=\>#\<User \_id: 509fc01d77bb1…

Interestingly, when executing the following code block (which is also  
used to fix the problem with the request specs) within the console, it  
works!

[2] pry(main)\> class ActionDispatch::Routing::RouteSet  
[2] pry(main)\* def default\_url\_options(options={})  
[2] pry(main)\* { :locale =\> I18n.default\_locale }  
[2] pry(main)\* end  
[2] pry(main)\* end  
=\> nil  
[3] pry(main)\> app.user\_path User.first  
=\> “/de/users/509fc01d77bb1e6a050000a0”

I’m not sure whether this would be a valid fix also for the console?? Do  
you guys experience the same behavior??
