Routing rspec with nested routes

Problem

You would like to add a routing rspec test in your rails application that tests for a nested route.
So while you have something like the following in your config/routes.rb file:

namespace :admin do
  resources :users do
    resources :audits, only: [:index]
  end
end

you want to add the following test for your route:

expect(get: '/admin/users/:user_id/audits').to route_to(controller: 'admin/audits', action: 'index')

but you are getting an error like the following:

The recognized options <{"controller"=>"admin/audits", "action"=>"index", "user_id"=>":user_id"}> did not match <{"controller"=>"admin/audits", "action"=>"index"}>, difference:.
       --- expected
       +++ actual
       @@ -1 +1 @@
       -{"controller"=>"admin/audits", "action"=>"index"}
       +{"controller"=>"admin/audits", "action"=>"index", "user_id"=>":user_id"}

Solution

You will need to change your rspec to the following using any number for the user_id:

expect(get: '/admin/users/42/audits').to route_to(controller: 'admin/audits', action: 'index', user_id: '42')

Solution adapted from here