首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何测试渲染状态:使用rescue_from时使用Rails4和RSpec的404

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

Stack Overflow用户
提问于 2013-08-03 05:46:09
回答 2查看 21.6K关注 0票数 21

我有一个带有“页面控制器”的Rails4应用程序。

当找不到页面时,show-方法抛出一个自定义的异常'PageNotFoundError‘。

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

render not foundPagesController的私有方法,如下所示:

代码语言:javascript
复制
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显示:

代码语言:javascript
复制
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)

因此,到目前为止,它看起来是my :status => :not_found works。

当我做curl -v http://0.0.0.0:3000/pages/readmef curl日志时

代码语言:javascript
复制
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的测试失败:

代码语言:javascript
复制
 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

代码语言:javascript
复制
$ 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,unicorn。在开发和生产模式下,一切都按预期工作。即使test.log也是正确的,但是测试失败了。

谁能告诉我为什么测试结果是200而不是404?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-05 04:01:31

尽管我对这个解决方案不是很满意,但至少这是一个变通的办法:

我将测试分成两个不同的规格。一个用于测试响应代码404 (使用GET而不是访问),另一个用于测试警报。第二个测试是必要的,因为即使render_views是在规范文件之上定义的,get也不会呈现视图。

代码语言:javascript
复制
  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
票数 14
EN

Stack Overflow用户

发布于 2014-12-11 18:52:45

RSpec 3+的另一种方法是测试异常:

代码语言:javascript
复制
it 'responds with a 404 if the page is not found' do
  expect { get(:show, id: 'bad_id') }.to raise_error(ActionController::RoutingError)
end
票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18026598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档