skip to content

A GoogleMock test warns "Uninteresting mock function call" for one method and fails with "Unexpected mock function call" for another. What separates the two, and how do NiceMock, NaggyMock and StrictMock change each?

level: seniorimportance: should knowfreq 55%

answer

  1. no EXPECT_CALL at all versus none matching
  2. unexpected is always an error
  3. wrappers touch only uninteresting calls
  4. plain mock behaves as naggy

basics

~20 s

An uninteresting call hits a method with no EXPECT_CALL; an unexpected call hits a method that has EXPECT_CALLs, none of which match. Unexpected calls always fail. NiceMock silences uninteresting calls, NaggyMock (the default) warns, StrictMock fails them.

solid answer

~40 s

GoogleMock separates the two by whether the test expressed interest in the method. **Uninteresting**: `x.Y()` has no `EXPECT_CALL(x, Y(...))` at all, so the default action runs and, by default, a warning is printed; the test does not fail. **Unexpected**: there are `EXPECT_CALL`s for `Y`, but none matches these arguments (or the matching one has retired); that is always a failure. The wrappers change only uninteresting calls: `NiceMock<T>` suppresses the warning, `NaggyMock<T>` prints it, and `StrictMock<T>` turns it into a failure. In GoogleTest 1.18 a plain mock still behaves like `NaggyMock`, and the `--gmock_default_mock_behavior` flag (0 nice, 1 naggy, 2 strict) changes that for a whole binary. To allow other arguments for an expected method, add a catch-all `EXPECT_CALL(m, Y(_)).Times(AnyNumber())` declared **before** the specific ones.

code

cpp · 27 lines
cpp
#include <gmock/gmock.h>
#include <gtest/gtest.h>

class Sensor { public: virtual ~Sensor() = default;
  virtual double ReadCelsius() const = 0; virtual bool SelfTest() = 0; };
class Actuator { public: virtual ~Actuator() = default;
  virtual void SetHeater(bool on) = 0; };
class MockSensor : public Sensor { public:
  MOCK_METHOD(double, ReadCelsius, (), (const, override));
  MOCK_METHOD(bool, SelfTest, (), (override)); };
class MockActuator : public Actuator { public:
  MOCK_METHOD(void, SetHeater, (bool on), (override)); };

void Tick(Sensor& s, Actuator& a, double target) {
  s.SelfTest();                              // no EXPECT_CALL: uninteresting
  a.SetHeater(s.ReadCelsius() < target);
}

using ::testing::Return;

TEST(Thermostat, WarmRoomKeepsHeaterOff) {
  MockSensor sensor;                         // plain mock = naggy
  MockActuator heater;
  EXPECT_CALL(sensor, ReadCelsius()).WillOnce(Return(25.0));
  EXPECT_CALL(heater, SetHeater(true));      // wrong: the room is warm
  Tick(sensor, heater, 20.0);
}

go deeper

for a junior

Know that a plain mock warns about calls to methods the test never mentioned and fails on calls that break a stated expectation.

for a middle

Define uninteresting and unexpected precisely and say which wrapper changes which, with the table of three mock types.

for a senior

Diagnose a mixed log like this one quickly, and fix unexpected calls with a correctly ordered catch-all rather than by changing the wrapper.

for a principal

Decide whether a codebase flips the binary-wide default with the flag or leaves wrapper choice to each test, given how strictness affects refactoring.

## Two different failure modes GoogleMock's cookbook stresses that *uninteresting* and *unexpected* calls are "very different" concepts, and the console output in the example shows both in one run. - A call `x.Y(...)` is **uninteresting** if the test set *not a single* `EXPECT_CALL(x, Y(...))`. The test said nothing about `Y`, and GoogleMock's philosophy is that saying nothing means no constraint. The default action runs (an `ON_CALL`, or a default value), and on a default mock a `GMOCK WARNING` is printed because the author may have forgotten a constraint. - A call `x.Y(...)` is **unexpected** if there *are* `EXPECT_CALL`s on `x.Y`, but none of the active ones matches the arguments. The test declared interest and the code did something else. **An unexpected call is always an error.** In the example, `SelfTest()` has no expectation, so it is uninteresting and only warns. `SetHeater(false)` is called while the only expectation demands `true`, so it is unexpected, and the run lists each expectation it tried with the mismatching argument. The never-satisfied `SetHeater(true)` expectation then fails again when the mock is destroyed. ## Reading the log in order A mixed GoogleMock log is quicker to triage when it is read as a sequence of distinct reports: 1. **`GMOCK WARNING: Uninteresting mock function call`** names a method the test never mentioned and the value returned (`Returns: false`). It is information, not a verdict; decide whether the call is expected behaviour or a forgotten constraint. 2. **`Unexpected mock function call`** is a failure at the moment of the call. Under it, "Google Mock tried the following 1 expectation" lists every candidate with the argument that did not match (`Expected arg #0: is equal to true`, `Actual: false`). 3. **`Mock function called more times than expected`** means a matching expectation was found but already saturated: the count is wrong, not the arguments. 4. **`Actual function call count doesn't match`** comes from the mock's destructor at the end of the test and lists expectations that were never satisfied. One bug often produces two reports, as here: the wrong argument shows up as an unexpected call and again as an unsatisfied expectation. Fix the first report and the second usually disappears. ## What the three wrappers change `NiceMock<T>`, `NaggyMock<T>` and `StrictMock<T>` are templates that derive from your mock class `T`, so they can be passed wherever `T` is accepted and forward constructor arguments to `T`. | Mock type | Uninteresting call | Unexpected call | |---|---|---| | `NiceMock<T>` | default action, no output | failure | | `NaggyMock<T>`, and a plain `T` | default action plus a warning | failure | | `StrictMock<T>` | failure | failure | The cookbook spells out the consequence: if a test fails with a default mock it also fails with a nice mock, and vice versa, so making a mock nice never changes the result. Making it strict can. ## Changing the default for a whole binary In GoogleTest 1.18 a mock declared without a wrapper behaves exactly like `NaggyMock<T>`; the cookbook calls nice mocks "not yet the default". The flag `--gmock_default_mock_behavior` accepts: 1. `0`: unwrapped mocks act as `NiceMock`; 2. `1`: they act as `NaggyMock` (the default); 3. `2`: they act as `StrictMock`. The flag is parsed by `InitGoogleMock()`, which the `gmock_main` library calls for you. `--gmock_verbose` (`info`, `warning`, `error`) controls only how much is printed, not whether a call fails. ## Caveats of the wrappers - They only affect methods declared with `MOCK_METHOD` **directly** in `T`; a mock method inherited from a base of `T` may not be affected, depending on the compiler. - Nesting, such as `NiceMock<StrictMock<MockFoo>>`, is not supported. - They may not work correctly if `T`'s destructor is not virtual. ## Allowing more calls without losing the check A nice mock does not help with unexpected calls. If `SetHeater(false)` is legitimate in some ticks, say so with a catch-all: ```cpp EXPECT_CALL(heater, SetHeater(_)).Times(AnyNumber()); // older: catch-all EXPECT_CALL(heater, SetHeater(true)); // newer: the claim ``` Order matters here. GoogleMock searches expectations from the newest to the oldest, so the specific expectation must be declared **after** the catch-all or it is shadowed. A method that must never run is written as `EXPECT_CALL(m, Y(_)).Times(0)`, which makes any call unexpected regardless of the wrapper.

  • Does wrapping the heater mock in NiceMock make the WarmRoomKeepsHeaterOff test pass?
    No. `NiceMock` only suppresses the warning for `SelfTest()`, which is uninteresting. `SetHeater(false)` is still unexpected because an `EXPECT_CALL` exists for `SetHeater` and none matches, and the unmet `SetHeater(true)` expectation still fails at destruction. Fix the expectation, not the wrapper.
  • How can a CI job make every unwrapped mock strict without editing the tests?
    Run the test binary with `--gmock_default_mock_behavior=2`, provided its `main` calls `InitGoogleMock()` (as `gmock_main` does). Unwrapped mocks then treat uninteresting calls as failures. Mocks explicitly wrapped in `NiceMock` keep their own setting, since the flag only supplies the default for mocks without a wrapper.

  • Expecting NiceMock to make a call with mismatched arguments pass
  • Calling every warning-producing mock call an unexpected call
  • Believing a plain mock is strict by default
  • Declaring the catch-all EXPECT_CALL after the specific one
  • Nesting NiceMock inside StrictMock to get mixed behaviour

Assumes GoogleTest 1.18, where an unwrapped mock behaves as NaggyMock and the documentation still calls nice mocks not yet the default.