class RSpec::Mocks::MessageExpectation

‘self` so that they can be chained together to form a fluent interface.
defined here can be used to configure how it behaves. The methods return
Represents an individual method stub or message expectation. The methods

def and_call_original

Returns:
  • (nil) - No further chaining is supported after this.

Other tags:
    Note: - This is only available on partial doubles.
def and_call_original
  and_wrap_original do |original, *args, &block|
    original.call(*args, &block)
  end
end

def and_raise(*args)

Other tags:
    Note: -

Returns:
  • (nil) - No further chaining is supported after this.

Overloads:
  • and_raise(exception_instance)
  • and_raise(ExceptionClass, message)
  • and_raise(ExceptionClass)
  • and_raise
def and_raise(*args)
  raise_already_invoked_error_if_necessary(__method__)
  self.terminal_implementation_action = Proc.new { raise(*args) }
  nil
end

def and_return(first_value, *values)

Returns:
  • (nil) - No further chaining is supported after this.

Overloads:
  • and_return(first_value, second_value)
  • and_return(value)
def and_return(first_value, *values)
  raise_already_invoked_error_if_necessary(__method__)
  if negative?
    raise "`and_return` is not supported with negative message expectations"
  end
  if block_given?
    raise ArgumentError, "Implementation blocks aren't supported with `and_return`"
  end
  values.unshift(first_value)
  @expected_received_count = [@expected_received_count, values.size].max unless ignoring_args? || (@expected_received_count == 0 && @at_least)
  self.terminal_implementation_action = AndReturnImplementation.new(values)
  nil
end

def and_throw(*args)

Returns:
  • (nil) - No further chaining is supported after this.

Overloads:
  • and_throw(symbol, object)
  • and_throw(symbol)
def and_throw(*args)
  raise_already_invoked_error_if_necessary(__method__)
  self.terminal_implementation_action = Proc.new { throw(*args) }
  nil
end

def and_wrap_original(&block)

Returns:
  • (nil) - No further chaining is supported after this.

Other tags:
    Note: - This is only available on partial doubles.
def and_wrap_original(&block)
  if RSpec::Mocks::TestDouble === @method_double.object
    @error_generator.raise_only_valid_on_a_partial_double(:and_call_original)
  else
    warn_about_stub_override if implementation.inner_action
    @implementation = AndWrapOriginalImplementation.new(@method_double.original_implementation_callable, block)
    @yield_receiver_to_implementation_block = false
  end
  nil
end

def and_yield(*args, &block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
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

def at_least(n, &block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def at_least(n, &block)
  raise_already_invoked_error_if_necessary(__method__)
  set_expected_received_count :at_least, n
  if n == 0
    raise "at_least(0) has been removed, use allow(...).to receive(:message) instead"
  end
  self.inner_implementation_action = block
  self
end

def at_most(n, &block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def at_most(n, &block)
  raise_already_invoked_error_if_necessary(__method__)
  self.inner_implementation_action = block
  set_expected_received_count :at_most, n
  self
end

def exactly(n, &block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def exactly(n, &block)
  raise_already_invoked_error_if_necessary(__method__)
  self.inner_implementation_action = block
  set_expected_received_count :exactly, n
  self
end

def never

Returns:
  • (MessageExpecation) - self, to support further chaining.
def never
  error_generator.raise_double_negation_error("expect(obj)") if negative?
  @expected_received_count = 0
  self
end

def once(&block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def once(&block)
  self.inner_implementation_action = block
  set_expected_received_count :exactly, 1
  self
end

def ordered(&block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def ordered(&block)
  self.inner_implementation_action = block
  additional_expected_calls.times do
    @order_group.register(self)
  end
  @ordered = true
  self
end

def thrice(&block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def thrice(&block)
  self.inner_implementation_action = block
  set_expected_received_count :exactly, 3
  self
end

def times(&block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def times(&block)
  self.inner_implementation_action = block
  self
end

def twice(&block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def twice(&block)
  self.inner_implementation_action = block
  set_expected_received_count :exactly, 2
  self
end

def with(*args, &block)

Returns:
  • (MessageExpecation) - self, to support further chaining.
def with(*args, &block)
  raise_already_invoked_error_if_necessary(__method__)
  if args.empty?
    raise ArgumentError,
          "`with` must have at least one argument. Use `no_args` matcher to set the expectation of receiving no arguments."
  end
  self.inner_implementation_action = block
  @argument_list_matcher = ArgumentListMatcher.new(*args)
  self
end