Tuesday, June 09, 2009

RSpec route_for & params_from with Nested Routes & Custom Methods

I was just writing some route specs for nested routes that used PUT methods and it took some searching and experimentation to get it to work. I didn't find a lot of relevant documentation, so hopefully this will help others.

#routes.rb

map.resources :accounts do |account|
account.resources :accounts_users,
:member => {:upgrade => :put, :downgrade => :put}
end



Testing the generator (route_for)

route_for(:controller => "accounts_users",
:action => "upgrade",
:account_id => "3",
:id => "5").should ==
{:path => "/accounts/3/accounts_users/5/upgrade",
:method => 'put'}


In this case rather than matching the output of route_for against a simple string, you match it against a hash with :path and :method

Testing the recognition (params_from)

params_from(:put,
"/accounts/3/accounts_users/1/upgrade").should ==
{:controller => "accounts_users", :action => "upgrade",
:id => "1", :account_id => "3"}

Labels: , ,

1 Comments:

At 8:26 AM , Blogger Unknown said...

This is documented as part of the 1.2.x series upgrade instructions.

That said, be warned that route_for isn't testing what it claims to be testing, currently. Here's more info on it.

https://rspec.lighthouseapp.com/projects/5645-rspec/tickets/843-route-spec-helpers-dont-do-what-they-claim#ticket-843-28

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home