Ignoring mock calls during setup phase

c++, googlemock, googletest, unit-testing

Solution

The annoying thing is that the necessary functions are there: `gmock/gmock-spec-builders.h` defines `Mock::AllowUninterestingCalls` and others to control the generation of warnings for a specific mock object. Using these functions, it should be possible to temporarily disable warnings about uninteresting calls.

That catch is, however, that these functions are private. The good thing is that class `Mock` has some template friends (e.g., `NiceMock`) that can be abused. So I created the following workaround:

namespace testing
{
// HACK: NiceMock<> is a friend of Mock so we specialize it here to a type that
// is never used to be able to temporarily make a mock nice. If this feature
// would just be supported, we wouldn't need this hack...
template<>
struct NiceMock<void>
{
    static void allow(const void* mock)
    {
        Mock::AllowUninterestingCalls(mock);
    }

    static void warn(const void* mock)
    {
        Mock::WarnUninterestingCalls(mock);
    }

    static void fail(const void* mock)
    {
        Mock::FailUninterestingCalls(mock);
    }
};

typedef NiceMock<void> UninterestingCalls;
}

This lets me access the private functions through the `UninterestingCalls` typedef.

Problem

I often face the problem that mock objects need to be brought in a certain state before the "interesting" part of a test can start. For example, let's say I want to test the following class: ``` struct ToTest { virtual void onEnable(); virtual void doAction(); }; ``` Therefore, I create the following mock class: ``` struct Mock : ToTest { MOCK_METHOD0(onEnable, void()); MOCK_METHOD0(doAction, void()); }; ``` The first test is that `onEnable` is called when the system that uses a `ToTest` object is enabled: ``` TEST(SomeTest, OnEnable) { Mock mock; // register mock somehow // interesting part of the test EXPECT_CALL(mock, onEnable()); EnableSystem(); } ``` So far, so good. The second test is that `doAction` is called when the system performs an action and is enabled. Therefore, the system should be enabled before the interesting part of the test can start: ``` TEST(SomeTest, DoActionWhenEnabled) { Mock mock; // register mock somehow // initialize system EnableSystem(); // interesting part of the test EXPECT_CALL(mock, doAction()); DoSomeAction(); } ``` This works but gives an annoying warning about an uninteresting call to `onEnable`. There seem to be two common fixes of this problem: - Using `NiceMock<Mock>` to suppress all such warnings; and - Add an `EXPECT_CALL(mock, onEnable())` statement. I don't want to use the first method since there might be other uninteresting calls that really should not happen. I also don't like the second method since I already tested (in the first test) that `onEnable` is called when the system is enabled; hence, I don't want to repeat that expectation in all tests that work on enabled systems. What I would like to be able to do is say that all mock calls up to a certain point should be completely ignored. In this example, I want expectations to be only checked starting from the "interesting part of the test" comment. Is there a way to accomplish this using Google Mock?

Original source