使用Rspec + Capybara在Rails中测试错误页面

Las*_*ert 24 ruby rspec ruby-on-rails capybara

在Rails 3.2.9中我有自定义错误页面定义如下:

# application.rb
config.exceptions_app = self.routes

# routes.rb
match '/404' => 'errors#not_found'
Run Code Online (Sandbox Code Playgroud)

哪个效果如预期.当我进入时config.consider_all_requests_local = false,development.rbnot_found在访问时获得了视图/foo

但是我如何用Rspec + Capybara测试呢?

我试过这个:

# /spec/features/not_found_spec.rb
require 'spec_helper'
describe 'not found page' do
  it 'should respond with 404 page' do
    visit '/foo'
    page.should have_content('not found')
  end
end
Run Code Online (Sandbox Code Playgroud)

当我运行此规范时,我得到:

1) not found page should respond with 404 page
  Failure/Error: visit '/foo'
  ActionController::RoutingError:
    No route matches [GET] "/foo"
Run Code Online (Sandbox Code Playgroud)

我该怎么测试呢?

编辑:

忘了提一下:我已经开始config.consider_all_requests_local = falsetest.rb

Lar*_*ter 30

test.rb中有问题的设置不仅仅是

consider_all_requests_local = false
Run Code Online (Sandbox Code Playgroud)

但是也

config.action_dispatch.show_exceptions = true
Run Code Online (Sandbox Code Playgroud)

如果设置此项,则应该能够测试错误.我无法在周围的过滤器中使用它.

请查看http://agileleague.com/blog/rails-3-2-custom-error-pages-the-exceptions_app-and-testing-with-capybara/