notaceo
notaceo

Reputation: 1093

No route found in rspec test for an existing route

rake routes shows:

 estimate_location GET      /estimate/location/:id(.:format)   estimate/location#show

And my rspec test:

it 'should re-direct to location/new from show' do
  e = FactoryGirl.create :estimate
  get estimate_location_path e
  expect(response.status).to eq(302)
end

And the console says:

   Failure/Error: get estimate_location_path e
     ActionController::RoutingError:
       No route matches {:controller=>"estimate/location", :action=>"/estimate/location/1"}

This does not make sense to me. There is a route, I passed an object (which Rails smartly grabs the ID from), but it's saying there is no such path??

Upvotes: 2

Views: 2068

Answers (3)

fionaom
fionaom

Reputation: 11

I had the same issue. The app using the Engine had no issues mapping the routes but rspec could not find the route.

Hours later, I rewrote my routes file from:

Authentication::Engine.routes.draw do namespace :api do namespace :v1 do post '/auth_token/:id', to: Authentication::Api::V1::AuthTokenController.action(:create), as: :auth_token end end end

to

Authentication::Engine.routes.draw do namespace :api do namespace :v1 do post '/auth_token/:id' => 'auth_token#create', as: :auth_token end end end

It seems rspec wasn't smart enough to reverse engineer the way the route was written in the first snippet back to the controller.

Upvotes: 0

Frederick Cheung
Frederick Cheung

Reputation: 84172

It looks like you are writing a controller spec (which rails calls functional tests)

In these tests the get, post etc methods expect the first argument to be the name of the action and the second a hash of options - they bypass routing (although they do check that the action is routable). You would instead do

get :show, id: e.id

In integration tests (request specs or feature specs) on the other hand you would use an actual path (and depending on the setup you would use either visit or get, post etc but they'd be different get methods)

Upvotes: 4

aforalegria
aforalegria

Reputation: 370

Try to rewrite your test

it 'should re-direct to location/new from show' do
  let(:estimate) {FactoryGirl.create(:estimate)}

  before {get estimate_location_path(estimate)}

  expect(response.status).to eq(302)
end

Upvotes: 0

Related Questions