Reputation: 4388
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 )
{
// Method #1
// 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.
const Parent& childObject = pObject.GetSecondMockedObject().
bool val = childObject.SomethingReturnsBool();
// Method #2
// This also throws an error
// bool val = pObject.GetSecondMockedObject().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!
Upvotes: 2
Views: 5471
Reputation: 331
Your SomeRealFunction(Parent pObject) you need to pass a reference or pointer because you lost your mock configuration on copy object.
My code follow run without problems:
#include <iostream>
#include <vector>
#include <assert.h>
#include "gtest/gtest.h"
#include "gmock/gmock.h"
using namespace std;
using namespace testing;
class Parent
{
public:
virtual ~Parent() {}
Parent() {}
virtual const Parent& GetSecondMockedObject() const { return *this; }
virtual const bool SomethingReturnsBool() const { return false; }
};
class MockObject : public Parent
{
public:
MOCK_CONST_METHOD0( GetSecondMockedObject, const Parent&() );
MOCK_CONST_METHOD0( SomethingReturnsBool, const bool() );
};
class MyRealObject
{
public:
// Definition of SomeRealFunction is part of the production code
void SomeRealFunction(const Parent& pObject )
{
std::cout << "parent = " << &pObject << std::endl;
const Parent& childObject = pObject.GetSecondMockedObject();
std::cout << "child = " << &childObject << std::endl;
bool val = childObject.SomethingReturnsBool();
std::cout << "val = " << val << std::endl;
}
};
TEST(mytest, tehet)
{
MockObject mockParentObj;
MockObject mockChildObj;
EXPECT_CALL(mockChildObj, SomethingReturnsBool() ).WillRepeatedly( Return( true ) );
EXPECT_CALL(Const(mockParentObj), GetSecondMockedObject() ).WillRepeatedly( ReturnRef( mockChildObj ) );
MyRealObject myobj;
myobj.SomeRealFunction(mockParentObj);
}
int main(int argc, char *argv[]) {
::testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
Upvotes: 2