【问题标题】:Custom 404 error routing with rspec使用 rspec 自定义 404 错误路由
【发布时间】: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


    【解决方案1】:

    正如终端的错误所示,请求还有一个名为not_found 的参数是通过路由设置的。我所要做的就是像这样将该参数传递给测试:

    it "renders 404 on unknown route" do
        get("/thisrouteiswrong").should route_to("errors#index", "not_found" => "thisrouteiswrong")
    end
    

    【讨论】:

      猜你喜欢
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 2013-08-01
      • 2016-01-13
      • 1970-01-01
      相关资源
      最近更新 更多