In order to discuss flexmock usage it is important to define the following terms:
- Stub: fake object that returns a canned value.
- Mock: fake object that returns a canned value and has an expectation, i.e. it includes a built-in assertion.
- Spy: watches method calls and records/verifies if the method is called with required parameters and/or returns expected values/exceptions.
Flexmock declarations follow a consistent style of the following three forms:
flexmock ( OBJECT ).COMMAND( ATTRIBUTE ).MODIFIER[.MODIFIER, ...]
flexmock ( OBJECT [, ATTRIBUTE=VALUE, ...] )
flexmock ( ATTRIBUTE=VALUE [, ATTRIBUTE=VALUE,...] )
OBJECT: Either a module, a class, or an instance of a class.
COMMAND: One of should_receive, should_call, or new_instances.
These create the initial expectation object.
ATTRIBUTE: String name of an attribute.
MODIFIER: One of several Expectation modifiers, such as with_args,
and_return, and_raise, or times.
While the order of modifiers is unimportant to flexmock, there is a preferred convention that will make your tests more readable.
with_args(), place it before
If using the
times() modifier (or its aliases:
place them at the end of the flexmock statement: