Posted 6 months ago. Visible to the public. Repeats.

RSpec: How to aggregate failures

RSpec >= 3.3 added aggregate_failures, which allows multiple failures in an example and list them all, rather than aborting on the first failure.

This can be used:

  • In the global configuration
  • With the tag :aggregate_failures (our preferred option in case every expectations should be aggregated)
  • With the method aggregate_failures

Below is an example, which shows why aggregating failures are useful.

Example

Copy
aggregate_failures do first_expectation_object = false second_expectation_object = false third_expectation_object = true expect(first_expectation_object).to be(true), "first expectation failed" # will fail expect(second_expectation_object).to be(true), "second expectation failed" # will fail expect(third_expectation_object).to be(true), "third expectation failed" # will pass end
Copy
(::) failed steps (::) Got 2 failures from failure aggregation block "multiple expectations": 1) first expectation failed 2) second expectation failed

As you can see, the test is not being interrupted by the first two falsy expectations, the third expectation passed without a message.

This can be handy if you are testing for multiple values, which map to one context, e. g. mail components, in order to provide better readability of your tests or to even make clear a tests fail reason due to some dependency:

Copy
aggregate_failures "email header" do expect(mail.subject).to match("Welcome") expect(mail.to).to contain_exactly("user@somedomain.de") expect(mail.from).to contain_exactly("welcome@makandra.de") end
Copy
first_recipient = method_that_creates_a_user aggregate_failures "mail sent" do expect(ActionMailer::Base.deliveries.last.to).to contain("user@somedomain.de") expect(first_recipient.email).to match("user@somedomain.de") end

In the last example first_recipient has been created in a different file, so it might not be obvious (in more complicated cases, imagine a list of more recipients) that method_that_creates_a_user actually creates a user with the mail address user@somedomain.com.

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Avatar
Jakob Scholz
Last edit:
5 months ago
by Besprechungs-PC
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Jakob Scholz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more