vendredi 5 juin 2015

Return a mocked object from a mocked object in Google mock

I have the following setup:

class MockObject : public Parent
{
public:
    MOCK_CONST_METHOD0( GetSecondMockedObject, const Parent&() );
    MOCK_CONST_METHOD0( SomethingReturnsBool, const bool() );
};

I have

MockObject mockParentObj;
MockObject mockChildObj;

// I create the following expectation on mockChildObj
EXPECT_CALL( mockChildObj, SomethingReturnsBool() ).WillRepeatedly( Return( true ) );

// I create the following expectation on mockParentObj
EXPECT_CALL( mockParentObject, GetSecondMockedObject() ).WillRepeatedly( ReturnRef( mockChildObj ) );



// I am going to use the parent mock object somewhere
realProductionObject.SomeRealFunction( mockParentObject );

// Definition of SomeRealFunction is part of the production code
SomeRealFunction( Parent pObject )
{
    // This should call the parent mock object which should return the child
    // mock object. Then on that object I call SomethingReturnsBool()
    // and the value of "val" should be true.
    Parent childObject = pObject.GetSecondMockedObject().
    bool val = childObject.SomethingReturnsBool();
}

However, when I execute the code( which is a bit different than this code and it compiles without an issue) I get the following exception and it is caused by the call to SomethingReturnsBool():

First-chance exception at 0x023CC193 in MyTest.exe: 0xC0000005: Access violation reading location 0xCCCCCCE8.
Critical error detected c0000374

I am suspecting that the child mock object reference returned from the call GetSecondMockObject() is invalid. I am not sure how else to pass it? I tried using: ReturnPointee( &... ) instead of ReturnRef( ... ) but that also didn't work.

I would appreciate any suggestions!

Aucun commentaire:

Enregistrer un commentaire