【问题标题】:Trying to use WithArg in GMock code; error says it doesn't exist尝试在 GMock 代码中使用 WithArg;错误说它不存在
【发布时间】:2015-10-02 18:30:59
【问题描述】:

我正在尝试在一些测试代码中使用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&lt;void(const std::basic_string&lt;char&gt;&amp;)&gt;’ has no member named ‘WithArg’

我在这里做错了什么?

【问题讨论】:

  • .WillRepeatedly(WithArg&lt;0&gt;(Invoke(this, &amp;TestClass::FunctionThatTakesAString))).Times(4)
  • @PiotrSkotnicki 这样做会导致运行时错误Failure .Times() cannot appear after .InSequence(), .WillOnce(), .WillRepeatedly(), or .RetiresOnSaturation().
  • Times移到WillRepeatedly之前
  • @PiotrSkotnicki 啊,但是将Times(4) 放在WillRepeatedly 之前确实有效。谢谢!
  • @PiotrSkotnicki 也许写一个答案?

标签: c++ unit-testing googlemock


【解决方案1】:

WithArg&lt;N&gt; 是一个动作适配器,而不是一个成员函数。要使用它,请将其作为 WillRepeatedly 子句中的操作:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-14
    • 2019-08-07
    相关资源
    最近更新 更多