Search code examples
c++pointersgoogletestgooglemock

Google Mock and SetArgPointee using Objects


I am recently using GMock and I cannot successfully use SetArgPointee in the following code.

class rox{
public :
    int a, b;
    rox(){}
    rox(const rox &ana){
        a = ana.a;
        b = ana.b;
    }
    rox operator=(const rox& ana){
        a = ana.a;
        b = ana.b;
        return *this;
    }
};

TEST(Statistics, RandomReceived){
    MockClass ms; 
    rox ana1, ana2;
    EXPECT_CALL(ms, Read(_)).Times(1).WillOnce(DoAll(SetArgPointee<0>(ana1), Return(1)));
    ms.Read(&ana2);
    EXPECT_EQ(ana2.a, ana1.a);
    EXPECT_EQ(ana2.b, ana1.b);
}

The test fails because the ana2 object after calling the read function does not have the field a and b properly set. Using a watch in VS I can see ana2.a and ana2.b are garbage values.

I read in GMock cook book I also need a copy constructor and an assignment operator. If I use the debugger I can see the copy constructor is called 4 times and the assignment operator is never called.

Can anyone point the error here? What do I need to do to obtain after the read in ana2 the same object as ana1?


Solution

  • SetArgPointee is not working with polymorphic objects :) If Read's signature is int Read(ARox *ana); and ARox is an abstract class and rox is inherited from ARox the expectations are not met