Unit testing Gmock-如何从输入参数设置模拟函数参数值?
我在我的项目中使用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
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))@你的评论很有帮助。把它构造成答案。