Search code examples
c++unit-testinggoogletestgooglemock

EXPECT_CALL without mock in Google Test


Is there any way to test a function call via GoogleTest for c++ without creating mock object, e.g. we have the following production code:

if (a)
    method(x);

I would like to test whether the method will be called in the case a is True and a is False. I would like to construct a test that does exactly the same what Google Test's EXPECT_CALL does, but EXPECT_CALL works only with the mock object's method. In my case I would prefer not to use a mock (there is no need to create any object).


Solution

  • As state here,

    It's possible to use Google Mock to mock a free function (i.e. a C-style function or a static method). You just need to rewrite your code to use an interface (abstract class).

    Their "It's possible" is misleading, as you have to rewrite code to use class (abstract, or provided by template), and so no longer use free functions.