Topic: Explaining assert_recognizes

I understand it is useful for testing things like this:
If I have a "resources :users"
If I send a GET request to the path "/users/1", will it translate into { :controller => "users", :action => "show", :id => 1} ?
This can be tested with the following code:
assert_recognizes({:controller => "users", :action => "show", :id => 1}, "/users/1")
In other words, we want to make sure that when a user navigates to the path provided in the 2nd parameter, that inside Rails he will be routed to the controller and action including the :id => 1, which are given in the 1st parameter. This assertion helps us do that.
When we omit the :id => 1 from the 1st parameter, we will get an error in our test case. That's ok.

Now, how do we test that the path "/users/1?locale=en" translates into:
{:controller => "users", :action => "show", :id => 1, :locale => :en} ?
The documentation warns us not to pass the ?locale=en inside the 2nd parameter, instead, we should pass it as the 3rd parameter, like this:
assert_recognizes({:controller => "users", :action => "show", :id => 1, :locale => :en}, "/users/1", { :locale => :en })
This test passes as well, everything seems ok so far.

Now the problem shows up if we omit the :locale => :en from the 1st parameter, like this:
assert_recognizes({:controller => "users", :action => "show", :id => 1}, "/users/1", { :locale => :en })
The problem being that this test passes as well! the assert_recognizes functions doesn't care if the keys-values passed in the "extras"/3rd parameter actually appear in the "expected options" / 1st parameter, whereas it cares if the parameter(s) passed implicitly in the path ("/users/1"), do exist - when we omit :id => 1, we will get an error.

Can somebody please explain how to make the "extras" parameter useful? … recognizes

Last edited by hardipe (2011-07-16 13:41:17)

Re: Explaining assert_recognizes

Did you by chance supply a default value for the locale in your routes.rb file?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.