Google Test’s mocking component (Google Mock) lets you tell the framework how many times a given method may be called:
EXPECT_CALL(mock, DoSomething(_)).Times(3);
The moment the method has been invoked the third time the expectation is said to be saturated.
By default the expectation stays alive even after it is saturated, and any further call that would
match it will result in an “unexpected call” failure.
RetiresOnSaturation( ) is a small but very handy modifier that changes this behavior:
EXPECT_CALL(mock, DoSomething(_))
.Times(3)
.RetiresOnSaturation(); // 👈 the magic
What it does
It still enforces the original call-count contract (the method must be called exactly three times).
As soon as the third call happens the expectation is removed (“retired”) from the active
expectation list.
Subsequent calls that would have matched the retired expectation are now matched by:
• another (more generic) EXPECT_CALL you have written, or
• the default ON_CALL(…) stub, or
• the built-in “uninteresting call” rule
instead of producing a failure.
Why you might need it
• You only care about the first N calls
code
// First call must return 42, afterward we don’t care.
EXPECT_CALL(db, Connect()).Times(1)
.WillOnce(Return(42))
.RetiresOnSaturation();
// All later connects should be silently ignored.
ON_CALL(db, Connect()).WillByDefault(Return(0));
• You want multiple expectations on the same method to be checked in sequence
• You need a strict check during start-up but loose behavior afterward
code
EXPECT_CALL(cache, WarmUp()).Times(2).RetiresOnSaturation();
ON_CALL(cache, WarmUp()).WillByDefault(Return()); // allow extra warm-ups
Common questions
Q: Does it hide unsatisfied expectations?
A: No. If the expectation never reaches its required call count, the test still fails. Retirement
happens only after saturation.
Q: Is it the same as Times(AtLeast(n))?
A: Not exactly. AtLeast(n) continues to check every further call and counts them toward the same
expectation. RetiresOnSaturation stops checking after the first n calls and lets something else
handle the rest.
Q: Can I combine it with InSequence or With?
A: Yes, it is just another clause in the EXPECT_CALL chain; it plays nicely with other matchers.
In short: add .RetiresOnSaturation() when you want to enforce “call this method N times, then I no longer care about it.”
User
can scope affect this RetiresOnSaturation ?
Assistant
Short answer
No – the C++ scope in which you write the EXPECT_CALL line has no influence on how
RetiresOnSaturation behaves.
What matters is the lifetime of the mock object, because expectations live as long
as the mock lives, not as long as the EXPECT_CALL statement is “in scope”.
Why scope doesn’t matter
• EXPECT_CALL is evaluated immediately and registers an internal
“expectation record” inside the mock object.
That record is heap-allocated and owned by the mock; it does not
disappear when the local block ends.
cpp
{
EXPECT_CALL(net, Connect())
.Times(1)
.RetiresOnSaturation(); // ← record is created here …
} // … but still alive after the brace
• RetiresOnSaturation simply changes a flag in that record saying
“remove me from the active-expectation list when I’m saturated.”
The flag remains valid as long as the record exists.
• The record is destroyed only when the mock’s destructor runs
(normally at the end of the test, or earlier if you create the mock
in an inner scope).
At that point Google Mock verifies that any un-saturated
expectations are still outstanding and fails the test if necessary.
What scope can influence
There is one indirect way scope affects things:
If the mock object itself has automatic storage duration inside a
nested block, its destructor is called as soon as that block ends.
If the expectation hasn’t been saturated yet, the test fails right
there—exactly the same behaviour you’d get without
RetiresOnSaturation.
• Whether you add RetiresOnSaturation inside or outside a local scope
makes no difference; once the expectation is registered, it stays
active until saturation or until the mock is destroyed.
• If you need the expectation to remain valid for later code, make sure
the mock object itself outlives that code (e.g., keep it as a class
member in a test fixture or allocate it on the heap).
• If you see a failure that looks like RetiresOnSaturation “didn’t
work,” check that the mock hasn’t gone out of scope too early.
User
can I use After for diff mocks?
Assistant
Yes 👍. After() works across any expectations, even when they belong to different mock
objects.
Internally Google Mock just stores dependency edges between “expectation nodes.”
Those nodes don’t care which mock created them, so you can freely mix-and-match.
Small demo
cpp
using ::testing::_;
using ::testing::Expectation;
TEST(CrossMockOrdering, Example) {
MockDB db;
MockNotifier notifier;
// 1. The first DB query must happen first …
Expectation first_query = EXPECT_CALL(db, Query("SELECT * FROM users"));
// 2. … only then are we allowed to send the notification …
EXPECT_CALL(notifier, Send("users loaded"))
.After(first_query);
// 3. … and finally the second query, which must wait for BOTH:
EXPECT_CALL(db, Query("SELECT * FROM logs"))
.After(first_query) // after #1
.After( // and also after #2
testing::ExpectationSet{first_query, // could list many expectations
EXPECT_CALL(notifier, Send("users loaded"))});
}
Things to remember
• You get an Expectation handle from each EXPECT_CALL.
You can pass one or several of those handles to .After(...).
Their mocks may be the same or completely different.
• .After() merely adds order constraints. It does not affect the
call counts, return actions, or RetiresOnSaturation() behaviour of
any expectation involved.
• If you need a long chain or a complex DAG of dependencies, consider
naming each Expectation so the code stays readable:
• Cross-thread caveat: the ordering is verified at mock-call time, not
at “real-world happens-before” time. If your mocks are invoked from
multiple threads, Google Mock serialises the checks internally, but
you still need proper synchronisation in the code under test to make
those ordering constraints meaningful.
Bottom line: go ahead and use After() to coordinate expectations on as
many different mocks as you like; it’s exactly what the feature is
there for.