从Google模拟中的模拟对象返回一个模拟对象

时间:2015-06-05 17:01:34

标签: c++ unit-testing googletest googlemock gmock

我有以下设置:

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

我有

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();
}

但是,当我执行代码时(它与此代码略有不同并且编译没有问题)我得到以下异常,它是由对SomethingReturnsBool()的调用引起的:

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

我怀疑从调用GetSecondMockObject()返回的子模拟对象引用无效。我不知道怎么通过呢?我试过用: ReturnPointee( &... )代替ReturnRef( ... ),但也无效。

我会很感激任何建议!

1 个答案:

答案 0 :(得分:1)

您的SomeRealFunction(Parent pObject)需要传递引用或指针,因为您在复制对象上丢失了模拟配置。

我的代码运行没有问题:

#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();
}