如何测试渲染状态:使用raising_from时,使用Rails4和RSpec进行404测试 [英] How to test render status: 404 with Rails4 and RSpec when using rescue_from

查看:85
本文介绍了如何测试渲染状态:使用raising_from时,使用Rails4和RSpec进行404测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Rails4 application with a 'PagesController'.

当找不到页面时,显示方法将引发自定义的异常'PageNotFoundError'.

The show-method throws a customized Exception 'PageNotFoundError' when a Page is not found.

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

On top of the controller I defined rescue_from PageNotFoundError, with: :render_not_found

render not foundPagesController的私有方法,看起来像:

render not found is a private method of PagesController and looks like:

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

开发模式下的rails-log显示:

The rails-log in development-mode shows:

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)

到目前为止,它可以显示我的:status =>:not_found正常工作.

So, it seams my :status => :not_found works, so far.

当我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
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /pages/readmef HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 404 Not Found
< X-Frame-Options: SAMEORIGIN

但是使用RSpec进行的以下测试失败:

But the following test with RSpec fails:

 it 'renders an error if page not found' do
    visit page_path('not_existing_page_321')
    expect(response.status).to eq(404)
    within( '.alert-error' ) do
      page.should have_content('Page not_existing_page_321 doesn\'t exist')
    end
  end

  1) PagesController renders an error if page not found
     Failure/Error: expect(response.status).to eq(404)

       expected: 404
            got: 200

一切看起来都很好,甚至test.log也显示404

Everything looks fine and even the test.log says 404

$ tail -f log/test.log
Started GET "/pages/not_existing_page_321" for 127.0.0.1 at 2013-08-03 09:48:13 +0200
Processing by PagesController#show as HTML
  Parameters: {"id"=>"not_existing_page_321"}
  Rendered pages/_page.haml (0.8ms)
  Rendered layouts/_navigation.haml (0.6ms)
  Rendered layouts/_messages.haml (0.2ms)
  Rendered layouts/_locales.haml (0.3ms)
  Rendered layouts/_footer.haml (0.6ms)
Completed 404 Not Found in 6ms (Views: 4.5ms)

我尝试了其他服务器,WebRICK,Thin,独角兽.一切都在开发和生产模式下按预期进行.甚至test.log也是正确的,但测试失败.

I tried different Servers, WebRICK, Thin, unicorn. Everything works as expected in development and production mode. Even the test.log is correct but the test fails.

有人可以告诉我为什么测试显示200而不是404吗?

Can anybody tell me why the test says 200 instead of 404?

推荐答案

尽管我对这种解决方案不太满意,但至少可以解决此问题:

Although I'm not very happy with this solution, at least this is a work around:

我将测试分为两个独立的规范.一种用于测试响应代码404(使用GET代替访问),另一种用于测试警报.第二个测试是必要的,因为get不会呈现视图-即使render_views是在spec文件的顶部定义的.

I splited the test into two separate specs. One for testing the response code 404 (with GET instead of visit) and a second to test the alert. The second test is necessary because get doesn't render the view - even if render_views is defined on top of the spec-file.

  it 'response with 404 if page not found' do
    get :show, { controller: 'pages', id: 'not_existing_page_321' }
    expect(response.status).to eq(404)
  end

  it 'renders an error-message if page not found and shows index' do
    visit page_path('page_not_found')
    within '.alert-error' do
      page.should have_content("Page page_not_found doesn't exist")
    end
  end

这篇关于如何测试渲染状态:使用raising_from时,使用Rails4和RSpec进行404测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆