wildabeast wildabeast - 3 months ago 11
Ruby Question

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

Here is my route from

routes.rb
:

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 }
end
end


But the output of the spec:

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

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


Why can't rspec find the route?

Answer

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.

Comments