Running Custom Code On A Method Call

If you need to run custom code when a method is called, you can use the WHEN_CALLED macro with DoInstead.

In the next example I want to change the behavior of the static method StaticIntCall. The original implementation Throws an exception. I want to change the behavior: When the method is called, I want to call a global function called MethodWasCalled that sets a global variable to true. First let’s define the global method: 

bool methodWasCalled= false;
void CALLBACK MethodWasCalled()
    methodWasCalled = true;


Now, let’s use DoInstead to change the behavior of the StaticIntCall to call our callback function:

TEST_F(Tests, SetGlobalVariableOnMethodCall)

    int result = ClassWithStaticCalls::StaticIntCall();



With DoInstead you can run any code you’d like. You can also give it arguments for the callback. Look for more usages in the installed examples.