【发布时间】:2013-01-21 14:51:57
【问题描述】:
在写问题时偶然发现了这个并找到了答案 - 因为这是一个常见的用例,我想我会分享它。直到一分钟前,我还遇到了以下问题:
我使用错误控制器为我的 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"}>.
【问题讨论】:
标签: ruby-on-rails rspec routing http-status-code-404