gpt4 book ai didi

c++ - gmock中saveArg和saveArgPointee有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:52 26 4
gpt4 key购买 nike

我正在 gmock 学习。现在我试图模拟名为“任务”的类,如下所示:

class MockTask : public Task
{
public:
MOCK_METHOD3(Execute, bool(std::set<std::string> &setDeviceIDs, int timeout, PACKET_DATA *Data));
};

而且我想在调用 task.excute 时保存结构 pdata,以便我可以验证 pdata-> 成员。这是我的代码:

PAKET_DATA data;
EXPECT_CALL(task, Execute(testing::_, testing::_, testing::_))
.WillOnce(testing::saveArg<2>(&data));
ASSERT_EQ(data->resultcode, 0);

这样对吗? saveArg 和 saveArgPointee 有什么区别?

最佳答案

正如您在 gmock doc 中所读到的那样:

SaveArg(pointer) Save the N-th (0-based) argument to *pointer.

SaveArgPointee(pointer) Save the value pointed to by the N-th (0-based) argument to *pointer.

对于您的情况,您应该使用 SaveArgPointee - 因为您要保存指向的数据 (PACKET_DATA *Data) - 而不是指针本身...

看这个例子:

struct SomeData { int a; };
class ISomeClass
{
public:
virtual ~ISomeClass() = default;
virtual void foo(SomeData*) = 0;
};

void someFunction(ISomeClass& a)
{
SomeData b{1};
a.foo(&b);
}

class SomeMock : public ISomeClass
{
public:
MOCK_METHOD1(foo, void(SomeData*));
};

要测试 someFunction,您需要检查传递给 foo 的 pointee:

TEST(TestSomeFoo, shallPassOne)
{
SomeData actualData{};
SomeMock aMock;
EXPECT_CALL(aMock, foo(_)).WillOnce(::testing::SaveArgPointee<0>(&actualData));
someFunction(aMock);
ASSERT_EQ(1, actualData.a);
}

如果您使用了 SaveArg - 您将只存储指向不再存在的局部变量的指针:

TEST(TestSomeFoo, shallPassOne_DanglingPointer)
{
SomeData* actualData;
SomeMock aMock;
EXPECT_CALL(aMock, foo(_)).WillOnce(::testing::SaveArg<0>(&actualData));
someFunction(aMock);
ASSERT_EQ(1, actualData->a);
}

关于c++ - gmock中saveArg和saveArgPointee有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39323381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com