Quantcast
Channel: Working with Rails
Viewing all articles
Browse latest Browse all 15

RSpec route_for & params_from with Nested Routes & Custom Methods

$
0
0
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"}


Viewing all articles
Browse latest Browse all 15

Trending Articles