Method: RSpec::Matchers#aggregate_failures

Defined in:
lib/rspec/matchers.rb

#aggregate_failures(label = nil, metadata = {}) { ... } ⇒ Object

Note:

The implementation of this feature uses a thread-local variable, which means that if you have an expectation failure in another thread, it'll abort like normal.

Allows multiple expectations in the provided block to fail, and then aggregates them into a single exception, rather than aborting on the first expectation failure like normal. This allows you to see all failures from an entire set of expectations without splitting each off into its own example (which may slow things down if the example setup is expensive).

Examples:

aggregate_failures("verifying response") do
  expect(response.status).to eq(200)
  expect(response.headers).to include("Content-Type" => "text/plain")
  expect(response.body).to include("Success")
end

Parameters:

  • label (String) (defaults to: nil)

    label for this aggregation block, which will be included in the aggregated exception message.

  • metadata (Hash) (defaults to: {})

    additional metadata about this failure aggregation block. If multiple expectations fail, it will be exposed from the Expectations::MultipleExpectationsNotMetError exception. Mostly intended for internal RSpec use but you can use it as well.

Yields:

  • Block containing as many expectation as you want. The block is simply yielded to, so you can trust that anything that works outside the block should work within it.

Raises:


305
306
307
# File 'lib/rspec/matchers.rb', line 305

def aggregate_failures(label=nil, ={}, &block)
  Expectations::FailureAggregator.new(label, ).aggregate(&block)
end