如何在Ruby on Rails系统测试中设置错误404页面

时间:2022-03-08 00:12:01

Rails 5.1 introduced system testing that uses Capybara with Selenium to test the UI of Rails application.

Rails 5.1引入了使用Capybara和Selenium来测试Rails应用程序UI的系统测试。

I'm wondering to how to use this system testing to test the UI of error pages.

我想知道如何使用这个系统测试来测试错误页面的UI。

For standard controller tests, we can do something like below to assert response to be 404.

对于标准控制器测试,我们可以执行类似下面的操作来声明响应为404。

test 'should get not_found' do
  get errors_not_found_url
  assert_response :not_found
end

But for system tests, if I go to a 404 page, exception will be thrown in controller level and tests terminate immediately without rendering the page.

但是对于系统测试,如果我转到404页面,将在控制器级别抛出异常并且测试会立即终止,而不会呈现页面。

test '404 page should render with the correct title' do
  # act.
  visit NOT_FOUND_URL

  # assert.
  assert_equal("#{APP_NAME} - #{TITLE_404}", page.title)
end

Exception is thrown in controller level.

在控制器级别抛出异常。

$ rails test test/system/error/error_page_test.rb
Run options: --seed 30076

# Running:

Puma starting in single mode...
* Version 3.9.1 (ruby 2.3.1-p112), codename: Private Caller
* Min threads: 0, max threads: 1
* Environment: test
* Listening on tcp://0.0.0.0:55237
Use Ctrl-C to stop
2017-07-09 11:10:45 +1200: Rack app error handling request { GET /books/12345678 }
#<ActionController::RoutingError: Could not find book '12345678' by id or name>
/myapp/app/controllers/books_controller.rb:7:in `index'
/Users/yze14/.rvm/gems/ruby-2.3.1/gems/actionpack-5.1.2/lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
/Users/yze14/.rvm/gems/ruby-2.3.1/gems/actionpack-5.1.2/lib/abstract_controller/base.rb:186:in `process_action'
...

Under development/test environment, config.consider_all_requests_local can be set to false in order to show error page instead of stracktrace. But this doesn't swallow exception during system tests.

在开发/测试环境下,config.consider_all_requests_local可以设置为false,以显示错误页面而不是stracktrace。但这不会在系统测试期间吞下异常。

1 个解决方案

#1


1  

If you don't want Capybara to re-raise server exceptions in the tests you can set Capybara.raise_server_errors = false.

如果您不希望Capybara在测试中重新引发服务器异常,则可以设置Capybara.raise_server_errors = false。

Secondly, you should check your Gemfile and make sure any gems like web-console,better-errrors, etc are only loaded in the development environment (not in the test environment)

其次,你应该检查你的Gemfile,并确保任何gems,如web-console,better-errrors等只在开发环境中加载(不在测试环境中)

Finally, you shouldn't be using assert_equal with title, you should be using the Capybara provided assert_title which includes waiting/retrying behavior and will reduce potential flakiness in tests.

最后,您不应该将assert_equal与title一起使用,您应该使用Capybara提供的assert_title,其中包括等待/重试行为,并将减少测试中的潜在flakiness。

assert_title("#{APP_NAME} - #{TITLE_404}")

#1


1  

If you don't want Capybara to re-raise server exceptions in the tests you can set Capybara.raise_server_errors = false.

如果您不希望Capybara在测试中重新引发服务器异常,则可以设置Capybara.raise_server_errors = false。

Secondly, you should check your Gemfile and make sure any gems like web-console,better-errrors, etc are only loaded in the development environment (not in the test environment)

其次,你应该检查你的Gemfile,并确保任何gems,如web-console,better-errrors等只在开发环境中加载(不在测试环境中)

Finally, you shouldn't be using assert_equal with title, you should be using the Capybara provided assert_title which includes waiting/retrying behavior and will reduce potential flakiness in tests.

最后,您不应该将assert_equal与title一起使用,您应该使用Capybara提供的assert_title,其中包括等待/重试行为,并将减少测试中的潜在flakiness。

assert_title("#{APP_NAME} - #{TITLE_404}")