Class RSpec::Mocks::MessageExpectation
In: lib/rspec/mocks/message_expectation.rb
Parent: Object

Methods

Attributes

argument_list_matcher  [W] 
error_generator  [RW]  @private
expected_from  [W] 
expected_received_count  [W] 
implementation  [RW]  @private
message  [R] 

Public Class methods

Public Instance methods

Tells the object to delegate to the original unmodified method when it receives the message.

@note This is only available on partial mock objects.

@example

  counter.should_receive(:increment).and_call_original
  original_count = counter.count
  counter.increment
  expect(counter.count).to eq(original_count + 1)

@overload and_raise @overload and_raise(ExceptionClass) @overload and_raise(ExceptionClass, message) @overload and_raise(exception_instance)

Tells the object to raise an exception when the message is received.

@note

  When you pass an exception class, the MessageExpectation will raise
  an instance of it, creating it with `exception` and passing `message`
  if specified.  If the exception class initializer requires more than
  one parameters, you must pass in an instance and not the class,
  otherwise this method will raise an ArgumentError exception.

@example

  car.stub(:go).and_raise
  car.stub(:go).and_raise(OutOfGas)
  car.stub(:go).and_raise(OutOfGas, "At least 2 oz of gas needed to drive")
  car.stub(:go).and_raise(OutOfGas.new(2, :oz))

@overload and_return(value) @overload and_return(first_value, second_value) @overload and_return(&block)

Tells the object to return a value when it receives the message. Given more than one value, the first value is returned the first time the message is received, the second value is returned the next time, etc, etc.

If the message is received more times than there are values, the last value is received for every subsequent call.

The block format is still supported, but is unofficially deprecated in favor of just passing a block to the stub method.

@example

  counter.stub(:count).and_return(1)
  counter.count # => 1
  counter.count # => 1

  counter.stub(:count).and_return(1,2,3)
  counter.count # => 1
  counter.count # => 2
  counter.count # => 3
  counter.count # => 3
  counter.count # => 3
  # etc

  # Supported, but ...
  counter.stub(:count).and_return { 1 }
  counter.count # => 1

  # ... this is prefered
  counter.stub(:count) { 1 }
  counter.count # => 1

@overload and_throw(symbol) @overload and_throw(symbol, object)

Tells the object to throw a symbol (with the object if that form is used) when the message is received.

@example

  car.stub(:go).and_throw(:out_of_gas)
  car.stub(:go).and_throw(:out_of_gas, :level => 0.1)

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

@example

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

Allows an expected message to be received any number of times.

Constrain a message expectation to be received at least a specific number of times.

@example

  dealer.should_receive(:deal_card).at_least(9).times

Constrain a message expectation to be received at most a specific number of times.

@example

  dealer.should_receive(:deal_card).at_most(10).times

Constrain a message expectation to be received a specific number of times.

@example

  dealer.should_receive(:deal_card).exactly(10).times

Expect a message not to be received at all.

@example

  car.should_receive(:stop).never

Expect a message to be received exactly one time.

@example

  car.should_receive(:go).once

Expect messages to be received in a specific order.

@example

  api.should_receive(:prepare).ordered
  api.should_receive(:run).ordered
  api.should_receive(:finish).ordered

Syntactic sugar for `exactly`, `at_least` and `at_most`

@example

  dealer.should_receive(:deal_card).exactly(10).times
  dealer.should_receive(:deal_card).at_least(10).times
  dealer.should_receive(:deal_card).at_most(10).times

Expect a message to be received exactly two times.

@example

  car.should_receive(:go).twice

Constrains a stub or message expectation to invocations with specific arguments.

With a stub, if the message might be received with other args as well, you should stub a default value first, and then stub or mock the same message using `with` to constrain to specific arguments.

A message expectation will fail if the message is received with different arguments.

@example

  cart.stub(:add) { :failure }
  cart.stub(:add).with(Book.new(:isbn => 1934356379)) { :success }
  cart.add(Book.new(:isbn => 1234567890))
  # => :failure
  cart.add(Book.new(:isbn => 1934356379))
  # => :success

  cart.should_receive(:add).with(Book.new(:isbn => 1934356379)) { :success }
  cart.add(Book.new(:isbn => 1234567890))
  # => failed expectation
  cart.add(Book.new(:isbn => 1934356379))
  # => passes

Protected Instance methods

[Validate]