Search code examples
unit-testingmockinggoogletestassertgooglemock

What is the oposite of EXPECT_CALL?


I have some expectations like EXPECT_CALL (...)

EXPECT_CALL(t1, foo()).Times(1);

I want to create the oposite. I expect that a certain function won't be executed.

What is the method I should use? Something like EXPECT_NOT_CALL (...) ?


Solution

  • In GTest something similar to EXPECT_NOT_CALL doesn't exist however, there are several options to receive this behaviour:

    1.Create a StrictMock. In StrictMock any unexpected called cause a failure.

    2.Use .Times(0):

    EXPECT_CALL(t1, foo()).Times(0);
    

    In this option you use the count mechanism however, it checks if the value is 0.(so any call leads a failure...)

    3.Use method 2 and create a Macro:

    #define EXPECT_NOT_CALL(a,b)     EXPECT_CALL(a, b).Times(0);