Method: RSpec::Matchers#yield_with_args

Defined in:
lib/rspec/matchers.rb

#yield_with_args(*args) ⇒ Object Also known as: a_block_yielding_with_args, yielding_with_args

Note:

Your expect block must accept a parameter and pass it on to the method-under-test as a block.

Note:

This matcher is not designed for use with methods that yield multiple times.

Given no arguments, matches if the method called in the expect block yields with arguments (regardless of what they are or how many there are).

Given arguments, matches if the method called in the expect block yields with arguments that match the given arguments.

Argument matching is done using === (the case match operator) and ==. If the expected and actual arguments match with either operator, the matcher will pass.

Examples:

expect { |b| 5.tap(&b) }.to yield_with_args # because #tap yields an arg
expect { |b| 5.tap(&b) }.to yield_with_args(5) # because 5 == 5
expect { |b| 5.tap(&b) }.to yield_with_args(Integer) # because Integer === 5
expect { |b| File.open("f.txt", &b) }.to yield_with_args(/txt/) # because /txt/ === "f.txt"

expect { |b| User.transaction(&b) }.not_to yield_with_args # because it yields no args
expect { |b| 5.tap(&b) }.not_to yield_with_args(1, 2, 3)


919
920
921
# File 'lib/rspec/matchers.rb', line 919

def yield_with_args(*args)
  BuiltIn::YieldWithArgs.new(*args)
end