As you can read in 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.
for your case, you should use SaveArgPointee
- as you want to save pointed data (PACKET_DATA *Data
) - not the pointer itself...
Look at this example:
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*));
};
To test someFunction
you need to check pointee that is being passed to foo
:
TEST(TestSomeFoo, shallPassOne)
{
SomeData actualData{};
SomeMock aMock;
EXPECT_CALL(aMock, foo(_)).WillOnce(::testing::SaveArgPointee<0>(&actualData));
someFunction(aMock);
ASSERT_EQ(1, actualData.a);
}
If you used SaveArg
- you would just store pointer to local variable that no longer exists:
TEST(TestSomeFoo, shallPassOne_DanglingPointer)
{
SomeData* actualData;
SomeMock aMock;
EXPECT_CALL(aMock, foo(_)).WillOnce(::testing::SaveArg<0>(&actualData));
someFunction(aMock);
ASSERT_EQ(1, actualData->a);
}