deitch
deitch

Reputation: 14581

How to test that a route does not exist in Rails 4.x

If I have a route not defined, how do I test that it returns a 404?

Here is the relevant part of routes.rb

resources :reservations, only: [:index,:create,:destroy]

You can create, destroy and list reservations, but not change them.

The test then is:

patch :update, id: @reservation, reservation: { somefield: "data" }
assert_response :missing

This should pass, since the lack of a route should return a 404. Instead, I get a UrlGenerationError:

ActionController::UrlGenerationError: No route matches {:action=>"update", :controller=>"reservations", :id=>"980190962", :reservation=>{:somefield=>"data"}}

I get it; the patch call from the test cannot generate a URL. So how do I test that such a URL call would generate a 404?

Upvotes: 3

Views: 879

Answers (1)

You actually could assert an error like this has been thrown, would it be sufficient for you?

assert_raise ActionController::UrlGenerationError do
  patch :update, id: @reservation, reservation: { somefield: "data" }
end

Upvotes: 4

Related Questions