gmock
EXPECT_CALL
Prototype
EXPECT_CALL(mock_object, method(matchers))
.Times(cardinality)
.WillOnce(action)
.WillRepetedly(action)
Arguments
matchers
an actual argument, a predefined matcher, or ‘_’ if not interestedcardinality
a predefined fuzzy cardinality(e.g. “AtLeast()”), or an actual number (incl. 0).-
action
there are following cases- Return type of
method
if built-in type or pointer or default-constructable type(since C++11), use thedefault action
(e.g. for void function just return, for bool function return false) - Use predefined functions
- Return type of
Methods
-
Times()
optional methods, if not specified following rule is evaluated to determine its cardinality:- If no
WillOnce()
norWillRepetedly()
is specified, cardinality is 1 - If exist n(n >= 1)
WillOnce()
but noWillRepetedly()
, cardinality is n - If exist n(n >= 0)
WillOnce()
and oneWillRepetedly()
, cardinality is AtLeast(n)
- If no
Order
-
Multiple rules for one method
If there are multiple
EXPECT_CALL
defined in one test case for the samemethod
, then the matching order is reverse (the new rule override the old). This allows user to define general expectations in test fixture and override some dedicated rules later in the test case. -
Multiple rules for different method
By default, the definition order doesn’t affect the evaluation order, in other word there is no strict evaluation order. However, if you want to specify strict order, define an
InSequence
object. If you want relative order, check the CookBook.
Comments