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

Methods

Attributes

error_generator  [RW] 
expected_from  [W] 
expected_received_count  [W] 
method_block  [W] 
sym  [R]  @private

Public Class methods

Public Instance methods

@overload and_raise @overload and_raise(ExceptionClass) @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 `new`. If the exception class
  initializer requires any parameters, you must pass in an instance and
  not the class.

@example

  car.stub(:go).and_raise
  car.stub(:go).and_raise(OutOfGas)
  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_recieve(:deal_card).at_least(9).times

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

@example

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

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

@example

  dealer.should_recieve(: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_recieve(:deal_card).exactly(10).times
  dealer.should_recieve(:deal_card).at_least(10).times
  dealer.should_recieve(: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]