Method: Mocha::Expectation#yields

Defined in:
lib/mocha/expectation.rb

#yields(*parameters) ⇒ Expectation

Modifies expectation so that when the expected method is called, it yields with the specified parameters.

May be called multiple times on the same expectation for consecutive invocations.

Examples:

Yield parameters when expected method is invoked.

object = mock()
object.expects(:expected_method).yields('result')
yielded_value = nil
object.expected_method { |value| yielded_value = value }
yielded_value # => 'result'

Yield different parameters on different invocations of the expected method.

object = mock()
object.stubs(:expected_method).yields(1).then.yields(2)
yielded_values_from_first_invocation = []
yielded_values_from_second_invocation = []
object.expected_method { |value| yielded_values_from_first_invocation << value } # first invocation
object.expected_method { |value| yielded_values_from_second_invocation << value } # second invocation
yielded_values_from_first_invocation # => [1]
yielded_values_from_second_invocation # => [2]

Parameters:

  • parameters (*Array)

    parameters to be yielded.

Returns:

See Also:


252
253
254
255
# File 'lib/mocha/expectation.rb', line 252

def yields(*parameters)
  @yield_parameters.add(*parameters)
  self
end