wildabeast wildabeast - 1 year ago 67
Ruby Question

"No route matches" error in rspec, but route shows up in rake routes

Here is my route from


resources :books, :except => [:new, :edit] do

post "pull" => "books#update", :data => { "pull" => true }

rake routes
shows things as expected

pull_api_v1_book POST /api/v1/books/:id/pull(.:format)
api/v1/books#update {:format=>:json, :data=>{"pull"=>true}}

Running a dev server, the route works as expected:

curl -X POST http://localhost:3000/api/v1/books/3/pull?auth_token=1234567890

My spec in in books_controller_spec.rb:

describe "pull api" do
it "should not fail" do
post :pull, params: { :id => 12 }

But the output of the spec:

Failure/Error: post :pull, params: { :id => 12 }

No route matches {:action=>"pull", :controller=>"api/v1/books", :id=>12}

Why can't rspec find the route?

Answer Source

Controller tests in rails don't actually invoke the routing, it's only syntactic sugar for calling the controller action. Your spec should call:

post :update, params: { :id => 12 }

If you actually want to test the route you should use a request spec.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download