使用 Rspec + Capybara 测试 Rails 中的错误页面 [英] Testing error pages in Rails with Rspec + Capybara

查看:26
本文介绍了使用 Rspec + Capybara 测试 Rails 中的错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

# application.rbconfig.exceptions_app = self.routes# 路线.rb匹配'/404' =>'错误#not_found'

按预期工作.当我在 development.rb 中设置 config.think_all_requests_local = false 时,我在访问 /foo<时得到 not_found 视图/p>

但是我如何使用 Rspec + Capybara 进行测试?

我已经试过了:

#/spec/features/not_found_spec.rb需要'spec_helper'描述未找到页面"做它应该以 404 页面响应"做访问'/foo'page.should have_content('未找到')结尾结尾

当我运行这个规范时,我得到:

1) 未找到页面应以 404 页面响应失败/错误:访问/foo"ActionController::RoutingError:没有路由匹配 [GET] "/foo"

我该如何测试?

忘记提及:我在 test.rb

中设置了 config.think_all_requests_local = false

解决方案

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

think_all_requests_local = false

还有

config.action_dispatch.show_exceptions = true

如果你设置了这个,你应该能够测试错误.我无法在环绕过滤器中使用它.

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

In Rails 3.2.9 I have custom error pages defines like this:

# application.rb
config.exceptions_app = self.routes

# routes.rb
match '/404' => 'errors#not_found'

Which works like expected. When I set config.consider_all_requests_local = false in development.rb I get the not_found view when visiting /foo

But how do I test this with Rspec + Capybara?

I've tried this:

# /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

When I run this spec I get:

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

How can I test this?

Edit:

Forgot to mention: I've set config.consider_all_requests_local = false in test.rb

解决方案

The problematic setting in the test.rb is not only the

consider_all_requests_local = false

but also

config.action_dispatch.show_exceptions = true

If you set this you should be able to test the error. I was not able to use it in an around filter.

Have a look for http://agileleague.com/blog/rails-3-2-custom-error-pages-the-exceptions_app-and-testing-with-capybara/

这篇关于使用 Rspec + Capybara 测试 Rails 中的错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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