Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Gmock-如何从输入参数设置模拟函数参数值?_Unit Testing_Googletest_Googlemock_Gmock - Fatal编程技术网

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

Unit testing Gmock-如何从输入参数设置模拟函数参数值?,unit-testing,googletest,googlemock,gmock,Unit Testing,Googletest,Googlemock,Gmock,我在我的项目中使用Gmock。我有以下模拟函数 MOCK_METHOD2(helper, bool(const std::string&, std::string*)); 目标是为所有模拟调用将参数1的值设置为参数2 我做了以下几件事 std::string temp; EXPECT_CALL( MockObj, helper(_,_)) .WillRepeatedly( DoAll(SaveA

我在我的项目中使用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)));
std::字符串温度;
期待你的电话(
MockObj,
助手(,))
威尔先生(
DoAll(保存参数(&temp),//将第一个参数保存到temp
SetArgPointee(temp),//将其分配给第二个arg
返回(true));

但是,我看到参数2设置为tmp的原始值,而不是保存的值。还有别的办法解决这个问题吗?我想使这个EXPECT_调用动态化,而不是执行SetArgPointee(“testval”)

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

ACTION(AssignArg0ToArg1) {
  *arg1 = arg0;
}

然后您可以将其与
返回(true)
结合使用。有关自定义操作的更多信息,请访问。

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

ACTION(AssignArg0ToArg1) {
  *arg1 = arg0;
}

然后您可以将其与
返回(true)
结合使用。有关自定义操作的更多信息,请访问。

感谢您指出。最后使用了一个带有Invoke-.willrepeated的lambda(DoAll(Invoke([](const std::string&input,std::string*output){*output=input;}),Return(true))@你的评论很有帮助。把它构造成答案。谢谢指点。最后使用了一个带有Invoke-.willrepeated的lambda(DoAll(Invoke([](const std::string&input,std::string*output){*output=input;}),Return(true))@你的评论很有帮助。把它构造成答案。