My nested resource are associated in the models and the routes.rb:
resources :foos do
resources :bars
end
My app is routing ok, but I can’t seem to prove any assertions about my nested routes:
describe BarsController do
it "should have a route to 'show'" do
assert_generates "/foos/bars/1",
{controller: :bars, action: :show, id: "1"}
end
Here is the error:
Failure/Error: assert_generates "/foos/bars/1",
ActionController::RoutingError:
No route matches {:controller=>"bars", :action=>"show", :id=>"1"}
How do I test these nested routes?
When you run
rake routesyou should see that your nested routes look something like this:So two parameters are required to match this route:
foo_idandid. Since you’re only supplying theid, it’s failing to find a route.Also, for testing routes in rspec, you may find it more useful to use the built-in
routes_tomatcher instead of falling back toassert_*, like this: