What is the difference between saveArg and saveArgPointee in gmock?

I study in gmock. Now I'm trying to make fun of a class called "task", for example:

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

And I want to save struct pdata when calling task.excute so that I can check the pdata-> member. This is my code:

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

It is right? And what is the difference between saveArg and saveArgPointee?

+4
source share
2 answers

As you can read in gmock doc :

SaveArg (pointer) Save the argument of the Nth (0) in the * pointer.

SaveArgPointee (pointer) Save the value pointed to by the Nth (0-based) argument to a * 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:

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);
}
+5
If I replace the test 
TEST(TestSomeFoo, shallPassOne)
{
    SomeData actualData{};
    SomeMock aMock;
    EXPECT_CALL(aMock, foo(_)).WillOnce(::testing::SaveArgPointee<0>(&actualData));
    someFunction(aMock);
    ASSERT_EQ(1, actualData.a);
}

as 

TEST(TestSomeFoo, shallPassOne)
{
    SomeData actualData{1};
    SomeMock aMock;
    EXPECT_CALL(aMock, foo(actualData)).Times(1);
    someFunction(aMock);
}

?

+1

Source: https://habr.com/ru/post/1653629/


All Articles