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

标签 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’

我在这里做错了什么?

最佳答案

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

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

关于c++ - 尝试在 GMock 代码中使用 WithArg;错误说它不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32913592/

相关文章:

c++ - Googlemock 在测试退出时错误地报告错误。我究竟做错了什么?

C++流作为重载运算符时的参数<<

c++ - 从 cin 中读取一个整数序列并将它们存储在一个 vector 中

Python 单元测试 : how do I test the argument in an Exceptions?

c# - 如何使 Assert.AreEqual 通过 POCO

c++ - GMOCK - 模拟一个对象及其内部模拟方法

python - 在 C++ 中嵌入 Python : persistence of interpreter across execution

c++ - 对未对齐的内存访问发出警告?

c# - 模拟的移动方法?

c++ - expect_call 中使用的 SaveArg 值