By default, Google Mock matches expectations in any order. To enforce a specific order, use one of the following methods:
1. The After Clause
Use Expectation objects to create dependencies between calls.
Expectation e = EXPECT_CALL(...);EXPECT_CALL(...).After(e);- For multiple dependencies, use
ExpectationSet to collect several Expectation objects and pass the set to .After().
2. Sequences
Use Sequence objects to group expectations into a chain.
- Create a
Sequence s; - Use
.InSequence(s) on multiple EXPECT_CALL statements to ensure they occur in the order they are written. - For strict ordering of all expectations in a scope, use the
InSequence dummy object.
Note: Modifying an ExpectationSet after it has been used in an .After() call does not change the existing dependency.
// Using After
using ::testing::Expectation;
Expectation init_x = EXPECT_CALL(foo, InitX());
EXPECT_CALL(foo, Bar()).After(init_x);
// Using InSequence scope
using ::testing::InSequence;
{
InSequence dummy;
EXPECT_CALL(foo, Step1());
EXPECT_CALL(foo, Step2());
}