C++ 如何忽略gtest DoAll()中的第一个操作
我声明一个函数C++ 如何忽略gtest DoAll()中的第一个操作,c++,reference,googletest,gmock,C++,Reference,Googletest,Gmock,我声明一个函数 void MyFunction(const std::wstring& inParameter, std::wstring& outParamater); 第一个参数是传入参数,第二个是value out参数,我想通过函数获得的值将通过outParameter传递出去 现在我把它锁起来了 MOCK_METHOD2(MyFunction, void(const std::wstring&, std::wstring&)); 但是,当我使用此模拟函
void MyFunction(const std::wstring& inParameter, std::wstring& outParamater);
第一个参数是传入参数,第二个是value out参数,我想通过函数获得的值将通过outParameter
传递出去
现在我把它锁起来了
MOCK_METHOD2(MyFunction, void(const std::wstring&, std::wstring&));
但是,当我使用此模拟函数时:
std::wstring firstStr = L"firstStr";
std::wstring test = L"test";
EXPECT_CALL(*myGmockInstance, MyFunction(firstStr, _)).Times(1).WillOnce(DoAll(firstStr, SetArgReferee<1>(test)));
std::wstring firstStr=L“firstStr”;
标准::wstring测试=L“测试”;
EXPECT_调用(*myGmockInstance,MyFunction(firstStr,)).Times(1).WillOnce(DoAll(firstStr,setargreference(test));
它不起作用
我也试过了
EXPECT_CALL(*myGmockInstance, MyFunction(_, _)).Times(1).WillOnce(DoAll(_, SetArgReferee<1>(test)));
EXPECT_调用(*myGmockInstance,MyFunction(,)).Times(1).WillOnce(DoAll(,setargreference(test));
或
EXPECT_调用(*myGmockInstance,MyFunction(,)).Times(1).WillOnce(DoAll(firstStr,setargreference(test));
或
EXPECT_调用(*myGmockInstance,MyFunction(,)).Times(1).WillOnce(DoAll(setargreference(firstStr),setargreference(test));
我知道
inParameter
是const
,因此我不能使用setargreference
。但是如何设置它的值,同时我可以设置输出参数的值?我认为你问题的标题很容易误导。据我所知,您只想为函数的第二个参数(输出参数)指定一些任意值。这是使用Invoke
执行此操作的方法:
using ::testing::_;
void assignStringToArg(const std::wstring&, std::wstring& outputStr,
const std::wstring expectedStr) {
outputStr = expectedStr;
}
class SomeMock {
public:
MOCK_METHOD2(MyFunction, void(const std::wstring&, std::wstring&));
};
TEST(xxx, yyy) {
SomeMock someMock;
std::wstring firstStr(L"aaabbbccc");
std::wstring secondStr(L"I should change upon MyFunction call ...");
std::wstring expectedSecondStr(L"xxxyyyzzz");
EXPECT_CALL(someMock, MyFunction(firstStr, _)).Times(1).WillOnce(Invoke(std::bind(
&assignStringToArg,
std::placeholders::_1,
std::placeholders::_2,
expectedSecondStr)));
someMock.MyFunction(firstStr, secondStr);
ASSERT_EQ(expectedSecondStr, secondStr);
}
请注意,提供给Invoke
的函数必须与您期望调用的函数具有相同的签名(这就是我使用bind
的原因)。您可以使用google宏ACTION\u P
获得相同的结果。我更喜欢使用Invoke
,只是因为它看起来更干净
由于您的案例非常简单,您也可以像以前一样使用setargreference
来完成:
EXPECT_CALL(someMock, MyFunction(firstStr, _)).Times(1).WillOnce(
SetArgReferee<1>(L"something"));
someMock.MyFunction(firstStr, secondStr);
ASSERT_EQ(L"something", secondStr);
这是一个相当愚蠢的例子,因为它将输出变量设置为L“something1”
,然后立即设置为L“something2”
using ::testing::_;
void assignStringToArg(const std::wstring&, std::wstring& outputStr,
const std::wstring expectedStr) {
outputStr = expectedStr;
}
class SomeMock {
public:
MOCK_METHOD2(MyFunction, void(const std::wstring&, std::wstring&));
};
TEST(xxx, yyy) {
SomeMock someMock;
std::wstring firstStr(L"aaabbbccc");
std::wstring secondStr(L"I should change upon MyFunction call ...");
std::wstring expectedSecondStr(L"xxxyyyzzz");
EXPECT_CALL(someMock, MyFunction(firstStr, _)).Times(1).WillOnce(Invoke(std::bind(
&assignStringToArg,
std::placeholders::_1,
std::placeholders::_2,
expectedSecondStr)));
someMock.MyFunction(firstStr, secondStr);
ASSERT_EQ(expectedSecondStr, secondStr);
}
EXPECT_CALL(someMock, MyFunction(firstStr, _)).Times(1).WillOnce(
SetArgReferee<1>(L"something"));
someMock.MyFunction(firstStr, secondStr);
ASSERT_EQ(L"something", secondStr);
EXPECT_CALL(someMock, MyFunction(firstStr, _)).Times(1).WillOnce(
DoAll(SetArgReferee<1>(L"something1"), SetArgReferee<1>(L"something2")));
someMock.MyFunction(firstStr, secondStr);
ASSERT_EQ(L"something2", secondStr);