Method: RSpec::Mocks::MessageExpectation#and_yield

Defined in:
lib/rspec/mocks/message_expectation.rb

#and_yield(*args) {|@eval_context = Object.new| ... } ⇒ MessageExpectation

Tells the object to yield one or more args to a block when the message is received.

Examples:

stream.stub(:open).and_yield(StringIO.new)

Yields:

  • (@eval_context = Object.new)

Returns:



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/rspec/mocks/message_expectation.rb', line 214

def and_yield(*args, &block)
  raise_already_invoked_error_if_necessary(__method__)
  yield @eval_context = Object.new if block

  # Initialize args to yield now that it's being used, see also: comment
  # in constructor.
  @args_to_yield ||= []

  @args_to_yield << args
  self.initial_implementation_action = AndYieldImplementation.new(@args_to_yield, @eval_context, @error_generator)
  self
end