小编Noc*_*ell的帖子

如何在使用rescue_from时使用Rails4和RSpec测试渲染状态:404

我有一个带有'PagesController'的Rails4应用程序.

当找不到页面时,show-method抛出一个自定义的异常'PageNotFoundError'.

在我定义的控制器之上 rescue_from PageNotFoundError, with: :render_not_found

render not found是一种私有方法,PagesController看起来像:

def render_not_found
  flash[:alert]=t(:page_does_not_exists, title: params[:id])
  @pages = Page.all
  render :index, status: :not_found #404
end
Run Code Online (Sandbox Code Playgroud)

rails-log in development-mode显示:

Started GET "/pages/readmef" for 127.0.0.1 at 2013-08-02 23:11:35 +0200
Processing by PagesController#show as HTML
  Parameters: {"id"=>"readmef"}
  ..
  Completed 404 Not Found in 14ms (Views: 12.0ms)
Run Code Online (Sandbox Code Playgroud)

所以,到目前为止它接缝我的:status =>:not_found有效.

当我curl -v http://0.0.0.0:3000/pages/readmef卷曲日志

curl -v http://localhost:3000/pages/readmef
* About to connect() to localhost port 3000 (#0)
*   Trying 127.0.0.1...
* connected
* …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails http-response-codes

20
推荐指数
3
解决办法
2万
查看次数

标签 统计

http-response-codes ×1

rspec ×1

ruby-on-rails ×1