Read more

Test that an exception or error page is raised in Capybara

Henning Koch
December 10, 2010Software engineer at makandra GmbH

You can use these step definitions:

Then /^I should not see an error$/ do
  (200 .. 399).should include(page.status_code)
end

Then /^I should see an error$/ do
  (400 .. 599).should include(page.status_code)
end
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Note that you need to tag the scenario with @allow-rescue to test that an error is shown like this

@allow-rescue
Scenario: Accessing the admin area requires a login
  When I go to the admin area
  Then I should see an error

These step definitions will not work for @javascript scenarios using the selenium web-driver

@allow-rescue @javascript
Scenario: Accessing the admin area requires a login
  When I go to the admin area
  Then I should see an error
  # This throws `Capybara::Driver::Base#status_code (Capybara::NotSupportedByDriverError)`
Posted by Henning Koch to makandra dev (2010-12-10 19:02)