How to test custom routes in controller with rspec

2017

I tried the solutions above but they did not work. I got:

ArgumentError:
   unknown keyword: location_id

It seems that RSpec now requires a params parameter. The corresponding call would look like this:

get(:index, params: { location_id: 123 })

Perhaps it helps if you declare your route like this?

get "packages/city/:location_id" => "packages#index"

Remember to provide a location_id param in specs, e.g. get :index, location_id: 1.


it's because you are not passing location_id

the route is defined to match:

/packages/city/:location_id

so in order to comply with it you need to do something like

get :index, location_id: 1234

Had the same issue with Controller spec:

# rake routes | grep media_order
teacher_work_media_orders PATCH /teacher/works/:work_id/media_orders(.:format)  teacher/media_orders#update

when I did:

# spec/controller/teacher/media_orders_controller
patch :update data: {}

I got

 Failure/Error: patch :update
 ActionController::UrlGenerationError:
   No route matches {:action=>"update", :controller=>"teacher/media_orders"}

but when I did

patch :update, work_id: 1234, data: {}

that worked