Search code examples
ruby-on-railsrspeccontrollerroutes

Rspec & Rails: how to test controller for 404 error? It gives "no route matches" error instead of 404


I want to test that application responds with 404 error and have code:

require 'spec_helper'

describe Admin::UsersController do

  describe "GET new" do
    before { get :new }
    it { should respond_with(404) }
  end
end

My routes:

namespace :admin, module: :admin do
  resources :users, except: [:new, :create]
end

When I ran local server in production mode it really responds with 404 error, but when I run rspec test, it gives me the following:

1) Admin::UsersController GET new shold not be successful
   Failure/Error: get :new
   ActionController::UrlGenerationError:
     No route matches {:action=>"new", :controller=>"admin/users"}
   # ./spec/controllers/admin_users_spec.rb:7:in `block (3 levels) in <top (required)>'

How can I test this correctly?

Updated with correct answer:

describe "GET new" do
  it "should not route to new" do
    expect(get: :new).not_to be_routable
  end
end

Solution

  • I think you can check it with helper { get :new }.should_not be_routable according to this

    Update from comment:

    Should be expect(:get => :new).not_to be_routable