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

时间:2015-10-02 18:31:00

标签: c++ unit-testing googlemock

我正在尝试在某些测试代码中使用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’

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

WithArg<N>是一个动作适配器,而不是一个成员函数。要使用它,请在WillRepeatedly子句中对其执行操作:

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