Skip to content

Instantly share code, notes, and snippets.

@gareth
Created February 13, 2011 17:26
Show Gist options
  • Save gareth/824864 to your computer and use it in GitHub Desktop.
Save gareth/824864 to your computer and use it in GitHub Desktop.
Step definition to test for the presence of error messages (and display discrepancies)
Then /^I should see( only)? the following error messages?( in order)?:$/ do |only, ordered, table|
# Uses Capybara's `all` CSS selector to find error messages in Rails' default
# error messages output format
errors = all(:css, "#errorExplanation li").map { |error| [error.text] }
errors_expected = table.raw
errors_actual = only ? errors : (errors & table.raw)
unless ordered
# Cucumber cares about the order of tables, If we don't then we need to sort
# both sets of messages using the same criteria
errors_actual = errors_actual.sort
errors_expected = errors_expected.sort
end
# To display the differences between expected and actual tables, Cucumber
# needs the table to have column headings (we only have one column)
Cucumber::Ast::Table.new([["Error"]] + errors_actual).diff!([["Error"]] + errors_expected)
end
# Sample ways to use this step
And I should see the following error messages:
| Start date can't be blank |
| Maximum attendance is not a number |
And I should see only the following error message:
| Email has already been taken |
And I should see the following error messages in order:
| Player 1 doesn't have enough credit |
| Player 3 has been banned |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment