Class FlexMock::Expectation
In: lib/flexmock.rb
Parent: Object

An Expectation is returned from each should_receive message sent to mock object. Each expectation records how a message matching the message name (argument to should_receive) and the argument list (given by with) should behave. Mock expectations can be recorded by chaining the declaration methods defined in this class.

For example:

  mock.should_receive(:meth).with(args).and_returns(result)

Methods

Included Modules

Test::Unit::Assertions

Attributes

expected_args  [R] 
mock  [R] 
order_number  [R] 

Public Class methods

Create an expectation for a method named sym.

Public Instance methods

and_return(*args, &block)

Alias for returns

Modifies the next call count declarator (times, never, once or twice) so that the declarator means the method is called at least that many times.

E.g. method f must be called at least twice:

  mock.should_receive(:f).at_least.twice

Modifies the next call count declarator (times, never, once or twice) so that the declarator means the method is called at most that many times.

E.g. method f must be called no more than twice

  mock.should_receive(:f).at_most.twice

Is this expectation eligible to be called again? It is eligible only if all of its count validators agree that it is eligible.

Does the expected argument match the corresponding actual value.

Does the argument list match this expectation‘s argument specification.

Validate the correct number of calls have been made. Called by the teardown process.

Declare that the method is never expected to be called with the given argument list. This may be modified by the at_least and at_most declarators.

Declare that the method is expected to be called exactly once with the given argument list. This may be modified by the at_least and at_most declarators.

Declare that the given method must be called in order. All ordered method calls must be received in the order specified by the ordering of the should_receive messages. Receiving a methods out of the specified order will cause a test failure.

If the user needs more fine control over ordering (e.g. specifying that a group of messages may be received in any order as long as they all come after another group of messages), a group name may be specified in the ordered calls. All messages within the same group may be received in any order.

For example, in the following, messages flip and flop may be received in any order (because they are in the same group), but must occur strictly after start but before end. The message any_time may be received at any time because it is not ordered.

   m = FlexMock.new
   m.should_receive(:any_time)
   m.should_receive(:start).ordered
   m.should_receive(:flip).ordered(:flip_flop_group)
   m.should_receive(:flop).ordered(:flip_flop_group)
   m.should_receive(:end).ordered

Declare that the method returns a particular value (when the argument list is matched).

  • If a single value is given, it will be returned for all matching calls.
  • If multiple values are given, each value will be returned in turn for each successive call. If the number of matching calls is greater than the number of values, the last value will be returned for the extra matching calls.
  • If a block is given, it is evaluated on each call and its value is returned.

For example:

 mock.should_receive(:f).returns(12)   # returns 12

 mock.should_receive(:f).with(String). # returns an
   returns { |str| str.upcase }        # upcased string

and_return is an alias for returns.

Declare that the method is called limit times with the declared argument list. This may be modified by the at_least and at_most declarators.

Declare that the method is expected to be called exactly twice with the given argument list. This may be modified by the at_least and at_most declarators.

Verify the current call with the given arguments matches the expectations recorded in this object.

Declare that the method should expect the given argument list.

Declare that the method can be called with any number of arguments of any type.

Declare that the method should be called with no arguments.

Declare that the method may be called any number of times.

[Validate]