如何重复模拟同一个函数并在每次调用中检查参数?

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

我有一个函数需要一个方法(一个与被测函数不同的函数)被模拟 8 次。它有 2 个参数。

假设我正在模拟一个函数 - void func(const char *a, const char *b) 此函数 func 在被测函数内部调用了 8 次。

我如何编写一个 gtest 来顺序模拟 8 个调用并通过检查每个调用的参数对被测函数进行单元测试。对于这个例子,我想验证所有 8 个调用中的值 a 和 b 并通过测试。每次都使用不同的参数调用所有八个调用。请帮助我。

它被埋在文档中: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).

因此,只需按照正确的顺序调用 8 次 EXPECT_CALL,并为每次调用设置预期的参数。