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

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

问题描述

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

 #application.rb 
config .exceptions_app = self.routes

#route.rb
match'/ 404'=> ‘errors#not_found’

其工作原理与预期的一样。当我在 development.rb 中设置 config.consider_all_requests_local = false 时,会得到 not_found 访问 / foo

时的视图

但是我该如何使用Rspec + Capybara进行测试? / p>

我已经尝试过:

 #/ spec / features / not_found_spec.rb 
要求'spec_helper'
描述'未找到的页面'做
它'应该以404页面响应'做
访问'/ foo'
页面。当我跑步时,
结束
结束

我得到的这个规范:

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

我该如何测试?



编辑:



忘记了提及:我已经在 test.rb

$ b中设置了 config.consider_all_requests_local = false $ b

解决方案

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

  consider_all_requests_local = false 

而且

  config.action_dispatch.show_exceptions = true 

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



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


测试

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天全站免登陆