我想测试应用程序响应 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
我想你可以和助手一起检查一下
{ get :new }.should_not be_routable
按照这个
评论更新:
应该是
expect(:get => :new).not_to be_routable
检查的答案很棒,除非你“捕获”404 错误。
如果这样做,您可以使用如下内容检查路由是否为 404:
expect(post: "/invalid-route").to route_to("application#render_endpoint_not_found", unmatched: "v1/panel/profiling/categories")
我的routes.rb包含这一行:
match "*unmatched", to: "application#render_endpoint_not_found", via: :all