如何忽略测试 DoAll() 中的第一个操作
How to ignore the first action in the gtest DoAll()
>I 声明一个函数
void MyFunction(const std::wstring& inParameter, std::wstring& outParamater);
第一个参数是传入参数,第二个是值输出参数,我想通过函数获取的值将通过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)));
它不起作用。
我也试过
EXPECT_CALL(*myGmockInstance, MyFunction(_, _)).Times(1).WillOnce(DoAll(_, SetArgReferee<1>(test)));
或
EXPECT_CALL(*myGmockInstance, MyFunction(_, _)).Times(1).WillOnce(DoAll(firstStr, SetArgReferee<1>(test)));
或
EXPECT_CALL(*myGmockInstance, MyFunction(_, _)).Times(1).WillOnce(DoAll(SetArgReferee<0>(firstStr), SetArgReferee<1>(test)));
我知道inParameter
是const
,所以我不能使用它SetArgReferee
。但是如何设置它的值,同时我可以为outParameter
设置值?
我认为你问题的标题很有误导性。据我了解,您只想为函数的第二个参数(输出参数(分配一些任意值。这是您可以使用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
的原因(。您可以使用谷歌宏ACTION_P
达到相同的结果。我更喜欢使用Invoke
,只是因为它对我来说看起来更干净。
由于您的情况相当简单,您也可以像以前尝试的那样使用SetArgReferee
来完成:
EXPECT_CALL(someMock, MyFunction(firstStr, _)).Times(1).WillOnce(
SetArgReferee<1>(L"something"));
someMock.MyFunction(firstStr, secondStr);
ASSERT_EQ(L"something", secondStr);
我只是认为如果您只想执行一个操作,则使用DoAll
毫无意义。 但是...如果你真的坚持:
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);
这是一个相当愚蠢的例子,因为它将输出变量设置为L"something1"
然后立即设置为L"something2"
。
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 如何在创建自定义迭代器时获得 std::p air 的第一个和第二个?
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- WinAPI 在单击第一个对话框上的按钮控件并销毁第一个对话框后创建第二个对话框
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- 使第二个类的构造函数成为第一个类中的友元函数
- 为什么第二个代码给出了预期的结果,而第一个代码却没有?
- 如何忽略测试 DoAll() 中的第一个操作
- 为什么这个函数将RAX推送到堆栈作为第一个操作
- 程序在用户的第一个输入时不执行任何操作(跳过程序中的整个代码)
- 每次我将第一个整数放入数组时,方法都会崩溃.操作不良