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.


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

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)

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

Labels: , ,


At 8:26 AM , Blogger Randy Harmon 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.



Post a Comment

Subscribe to Post Comments [Atom]

<< Home