Search code examples
c++googletest

How to repeatedly mock the same function and check for the parameters in each call?


I have a function which expects a method(a different function other than the function under test) to be mocked 8 times. It has 2 arguments.

Lets say I am mocking a function - void func(const char *a, const char *b) This function func is called inside the function under test 8 times.

How can i write a gtest to sequentially mock the 8 calls and unit test the function under test by checking the parameters of each call. For this example i want to verify the values a and b in all 8 calls and pass the test. All the eight calls are being called with different parameters each time. Kindly help me.


Solution

  • It's buried in the docs here: https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#knowing-when-to-expect

    EXPECT_CALL not only defines the behavior, but also sets an expectation that the method will be called with the given arguments, for the given number of times (and in the given order when you specify the order too).

    So just setup your expectation with 8 invocations of EXPECT_CALL in the proper order with the expected arguments for each call.