[英]Testing error pages in Rails with Rspec + Capybara
In Rails 3.2.9 I have custom error pages defines like this:在 Rails 3.2.9 中,我有这样的自定义错误页面定义:
# 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
当我在
development.rb
中设置config.consider_all_requests_local = false
时,我在访问/foo
时得到了not_found
视图
But how do I test this with Rspec + Capybara?但是如何使用 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
忘了提:我在
test.rb
中设置了config.consider_all_requests_local = false
The problematic setting in the test.rb is not only the test.rb中有问题的设置不仅仅是
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/ 请查看http://agileleague.com/blog/rails-3-2-custom-error-pages-the-exceptions_app-and-testing-with-capybara/
the config.consider_all_requests_local = false
setting would need to be set in config/environments/test.rb
in the same way you have done for your development one. config.consider_all_requests_local = false
设置需要在config/environments/test.rb
中设置,方法与开发方式相同。
If you don't want to do this for all tests, perhaps an rspec around filter would be useful to set the state before the test and restore afterwards like so: 如果您不想为所有测试做到这一点,也许是周围RSpec的过滤器将在测试之前设置的状态和事后恢复像这样有用:
# /spec/features/not_found_spec.rb
require 'spec_helper'
describe 'not found page' do
around :each do |example|
Rails.application.config.consider_all_requests_local = false
example.run
Rails.application.config.consider_all_requests_local = true
end
it 'should respond with 404 page' do
visit '/foo'
page.should have_content('not found')
end
end
如果您想这样做并且不想更改config/environments/test.rb
,则可以按照此帖子中的解决方案进行操作。
With Rails 5.2, Capybara 3 I was able to simulate page errors with the following 使用Rails 5.2,Capybara 3我可以使用以下方法模拟页面错误
around do |example|
Rails.application.config.action_dispatch.show_exceptions = true
example.run
Rails.application.config.action_dispatch.show_exceptions = false
end
before do
allow(Person).to receive(:search).and_raise 'App error simulation!'
end
it 'displays an error message' do
visit root_path
fill_in 'q', with: 'anything'
click_on 'Search'
expect(page).to have_content 'We are sorry, but the application encountered a problem.'
end
This seems to not always work when running the full test suite. 运行完整的测试套件时,这似乎并不总是有效。 So I had to set
config.action_dispatch.show_exceptions = true
in config/environments/test.rb
and remove the around block. 所以我必须在
config/environments/test.rb
设置config.action_dispatch.show_exceptions = true
并删除around块。
You can access directly 404 error page: 您可以直接访问404错误页面:
Visit /404
instead of /foo
访问
/404
而不是/foo
The variables from Rails.application.config
are read at the start of the test suite to a separate hash ( Rails.application.env_config
) - so changing the source variables for a particular spec doesn't work. Rails.application.config
中的变量在测试套件开始时被读取到单独的 hash ( Rails.application.env_config
) - 因此更改特定规范的源变量不起作用。
I was able to solve this with the following around block - this allows testing the exception rescue without impacting the rest of the test suite我能够通过以下围绕块解决这个问题 - 这允许在不影响测试套件的 rest 的情况下测试异常救援
around do |example|
orig_show_exceptions = Rails.application.env_config['action_dispatch.show_exceptions']
orig_detailed_exceptions = Rails.application.env_config['action_dispatch.show_detailed_exceptions']
Rails.application.env_config['action_dispatch.show_exceptions'] = true
Rails.application.env_config['action_dispatch.show_detailed_exceptions'] = false
example.run
Rails.application.env_config['action_dispatch.show_detailed_exceptions'] = orig_detailed_exceptions
Rails.application.env_config['action_dispatch.show_exceptions'] = orig_show_exceptions
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.