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

Noc*_*ell 20 rspec ruby-on-rails http-response-codes

我有一个带有'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
* 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
Run Code Online (Sandbox Code Playgroud)

但是RSpec的以下测试失败了:

 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
Run Code Online (Sandbox Code Playgroud)

一切看起来都很好,甚至test.log说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)
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的服务器,WebRICK,瘦,独角兽.在开发和生产模式中,一切都按预期工作.即使test.log也是正确的,但测试失败了.

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

Jus*_*ner 17

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

it 'respond with 404 if page not found' do
  expect{ get :show, :id => 'bad_id' }.to raise_error(ActionController::RoutingError)
end
Run Code Online (Sandbox Code Playgroud)


Noc*_*ell 13

虽然我对这个解决方案不是很满意,但至少这是一个解决方法:

我将测试分成两个单独的规格.一个用于测试响应代码404(使用GET而不是访问),第二个用于测试警报.第二个测试是必要的,因为get不渲染视图 - 即使render_views是在spec文件的顶部定义.

  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
Run Code Online (Sandbox Code Playgroud)


Luc*_*ler 8

这里的问题是你混淆了Capybara功能测试和RSpec控制器测试.visit是由Capybara提供的方法和get/ response由RSpec控制器测试提供 - 您不能一起使用它们.

要将此测试作为单个RSpec控制器测试,您可以执行以下操作:

it "returns a not found response" do
  get :show, { id: 'not_existing_page_321' }
  expect(response.status).to eq(404)
  expect(response.text).to match(/Page page_not_found doesn't exist/)
end
Run Code Online (Sandbox Code Playgroud)

(Nb get line与你发布的不一样 - 我没有把controllerparam 包括在内,好像你把spec/controllers/pages_controller_spec.rb它放在它所属的地方你不需要那个)

或者作为单个Capybara功能测试:

it "renders a not found response" do
  visit page_path('page_not_found')
  expect(page.status_code).to eq(404)
  within '.alert-error' do
    expect(page).to have_content("Page page_not_found doesn't exist")
  end
end
Run Code Online (Sandbox Code Playgroud)