在发现问题时偶然发现了这个并找到了答案 - 因为这是一个常见的用例,我想我会分享它。直到一分钟前,我遇到了以下问题:
我使用错误控制器为我的rails应用程序设置了一个自定义404页面,其中包含以下代码:
def index
render "errors/index", status: 404
end
每当访问不存在的路由时,我都设置了路由来呈现此404页面:
devise_for :users
get "errors/index", as: :error
get "*not_found", to: "errors#index" # this is the important line
root to: "welcome#index"
事实上,它确实有效。但是,出于某种原因,我的规范不起作用:
it "renders 404 on unknown route" do
get("/thisrouteiswrong").should route_to("errors#index")
end
终端中生成的输出为:
The recognized options <{"controller"=>"errors", "action"=>"index", "not_found"=>"thisrouteiswrong"}> did not match <{"controller"=>"errors", "action"=>"index"}>, difference: <{"not_found"=>"thisrouteiswrong"}>.
<{"controller"=>"errors", "action"=>"index"}> expected but was
<{"controller"=>"errors", "action"=>"index", "not_found"=>"thisrouteiswrong"}>.
答案 0 :(得分:3)
当终端的错误显示时,请求还有一个名为not_found
的参数,它通过路由设置。我所要做的就是将该参数传递给测试,如下所示:
it "renders 404 on unknown route" do
get("/thisrouteiswrong").should route_to("errors#index", "not_found" => "thisrouteiswrong")
end