Rspec& Rails:如何测试控制器的404错误?它给出了#34;没有路线匹配"错误而不是404

时间:2014-05-24 11:10:48

标签: ruby-on-rails rspec controller routes

我想测试该应用程序响应404错误并且有代码:

require 'spec_helper'

describe Admin::UsersController do

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

我的路线:

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

当我在生产模式下运行本地服务器时,它确实以404错误响应,但是当我运行rspec测试时,它给了我以下内容:

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)>'

如何正确测试?

更新了正确答案:

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

1 个答案:

答案 0 :(得分:4)

我认为您可以使用帮助{ get :new }.should_not be_routable according to this

进行检查

评论更新:

应为expect(:get => :new).not_to be_routable