尝试在 GMock 代码中使用 WithArg;错误说它不存在

Trying to use WithArg in GMock code; error says it doesn't exist

我正在尝试在一些测试代码中使用 WithArg。我尝试编译的代码如下所示:

using ::testing::_;
using ::testing::Invoke;
using ::testing::WithArg;

EXPECT_CALL(myMock, MockMethodThatTakesAString(_))
                   .WithArg<0>(Invoke(this, &TestClass::FunctionThatTakesAString))
                   .Times(4);

当我尝试编译它时,出现错误

error: ‘class testing::internal::TypedExpectation<void(const std::basic_string<char>&)>’ has no member named ‘WithArg’

我做错了什么?

WithArg<N>是动作适配器,不是成员函数。要使用它,请将其作为 WillRepeatedly 子句中的一个动作:

EXPECT_CALL( myMock, MockMethodThatTakesAString(_) )
       .Times(4)
       .WillRepeatedly(WithArg<0>(Invoke(this
                                       , &TestClass::FunctionThatTakesAString)));