Testing for Non-Routes in Rails

Here’s the setup. We have a Store Controller which allows all actions except Edit, so we want to ensure that Store#Edit is not a valid route. Another way of stating our intention is that we’re testing for a No Route Matches.

Here’s what it looks like in routes.rb where Store is a nested resource of Users.

resources :users do
resources :stores, only: [:index, :new, :create, :destroy]
resources :coupons
end

Here’s our RSpec method located in our stores_controller_spec.rb file.

describe “GET ‘edit'” do
it “should not be routeable” do
expect(:get => “/users/1/stores/1/edit”).not_to be_routable
end
end

We’re using RSpec’s be_routable matcher. Here’s a good be_routable reference on Relishapp.

It would be nice to write the expectation in a more Ruby-esque fashion, perhaps

expect(get :edit, :user_id => user.id, :id => 1).not_to be_routable

but as far as I know we have to enter a string path. Otherwise, since Rails doesn’t know about the route we’d get a NoMethodError. We could test for the error, of course, but be_routable seems cleaner.