Rspec & Rails: how to check controller for 404 error? It gives the error “no route matches” instead of 404

I want to check that the application responds with 404 error and has the 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 started the local server in production mode, it really responds to 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 check this correctly?

Updated with the correct answer:

describe "GET new" do
  it "should not route to new" do
    expect(get: :new).not_to be_routable
  end
end
+4
source share
1 answer

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

Update from comment:

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

+4
source

Source: https://habr.com/ru/post/1541870/


All Articles