Gmock - 如何从输入参数设置模拟函数参数值?

Gmock - How to set mock function parameter value from an input parameter?

我在我的项目中使用 Gmock。我有以下模拟功能。

  MOCK_METHOD2(helper,
     bool(const std::string&, std::string*));

目标是为所有模拟调用将参数 1 的值设置为参数 2。

我做了以下事情。

  std::string temp;

  EXPECT_CALL(
        MockObj,
        helper(_,_))
        .WillRepeatedly(
          DoAll(SaveArg<0>(&temp), //Save first arg to temp
                SetArgPointee<1>(temp), //assign it to second arg
                Return(true)));

但是我看到参数2设置为tmp的原始值而不是保存的值。还有其他方法可以解决这个问题吗?我想让这个 EXPECT_CALL 动态化而不是执行 SetArgPointee<1>("testval")。

标准操作不支持您的需要。您将需要编写自定义匹配器:

ACTION(AssignArg0ToArg1) {
  *arg1 = arg0;
}

然后您可以将它与 Return(true) 结合使用。有关自定义操作的更多信息,请访问 https://github.com/google/googlemock/blob/master/googlemock/docs/DesignDoc.md