Home > Blockchain >  How To Properly Call a Method From a Mocked Method in Google Test/Mock
How To Properly Call a Method From a Mocked Method in Google Test/Mock

Time:08-31

In Gmock, I'm trying to get a mocked method to sleep for a few milliseconds and then call a method in the Class under Test. Here is an example of what I'm trying to do:

EXPECT_CALL(mockedClass, mockedMethod())
        .Times(1)
        .WillOnce(DoAll(Wait(100), ClassUnderTest.MethodToCall()));

I've defined Wait() a little higher up as:

ACTION_P(Wait, ms) { Sleep(ms); }

The problem is that I can't seem to get away from this compiler error:

Error C2664: cannot convert argument 2 from 'const Action2' to 'const testing::Action<F>&'

I just started using Google Test/Mock recently and nothing I've tried or can find seems to do anything about the problem.

Can anyone help me understand how to properly call a method from a mocked method in Google Test/Mock?

CodePudding user response:

Try using InvokeWithoutArgs, smth like this:

EXPECT_CALL(mockedClass, mockedMethod())
        .WillOnce(DoAll(Wait(100), InvokeWithoutArgs([&ClassUnderTest]() { ClassUnderTest.MethodToCall(); } ));

Times(1) is not needed with WillOnce.

CodePudding user response:

You should be able to do both function calls with InvokeWithoutArgs:

EXPECT_CALL(mockedClass, mockedMethod())
        .Times(1)
        .WillOnce(DoAll(InvokeWithoutArgs(Sleep(100)),
                        InvokeWithoutArgs(ClassUnderTest.MethodToCall())));
  • Related