尝试模拟接口(interface)时,似乎无法使用虚拟继承。我需要使用虚拟继承,因为我的代码依赖于第三方库。第三方库使用虚拟继承通过父级将一个 child 投射到另一个 child 。我们需要能够根据第三方库的期望进行模拟,但事实并非如此。

注意:IA,IB,IC和ID是第三方库

class IA
{
};

class IB :virtual public IA
{
};

class IC :virtual public IB
{
public:
    int getValue(){}
};

class ID : virtual public IA
{
public:
    void type(){};
};

class MockD :
              public ID
    //virtual public ID; fails all of my tests that depends on ID
{
public:
    MOCK_METHOD0(type, void())
};

class MockC :
    //virtual public IC, virtual public MockD{}; //throws an exception for accessing pD->type()
    //        public IC, virtual public MockD{}; //throws an exception for accessing pD->type()
    //virtual public IC,         public MockD{}; //throws an exception for accessing pC->getValue()
    //        public IC,         public MockD{}; //throws an exception for accessing pD->type()
{
    MOCK_METHOD0(getValue, void());

};
//throws an exception for accessing pD->type()


           IA
        _______
    (v)/       \(v)
      IB        ID
   (v)|         |
     IC       MockD
      |         |
    (v)\        /(v)
         MockC


           IA
        _______
    (v)/       \(v)
      IB        ID
   (v)|         |
     IC       MockD
      |         |
      \        /(v)
         MockC


           IA
        _______
    (v)/       \(v)
      IB        ID
   (v)|         |
     IC       MockD
      |         |
      \        /
         MockC

//throws an exception for accessing pC->getValue()

           IA
        _______
    (v)/       \(v)
      IB        ID
   (v)|         |
     IC       MockD
      |         |
   (v)\        /
         MockC
           IA

要测试的代码
ID* pD = dynamic_cast<ID*>(pC);
pD->type();
pC->getValue();

//在测试中
MockC* mockC = new MockC();
EXPECT_CALL(mockC, type());
EXPECT_CALL(mockC, getValue());

最佳答案

您只需要从ID派生的Mock对象并模拟其所有功能。然后忘掉MockC并以您需要的方式定义MockD:

class MockD : public ID
{
public:
    MOCK_METHOD0(type, void());
    MOCK_METHOD0(getValue, int());
};

并以这种方式设置测试期望:
MockD mockD;
EXPECT_CALL(mockD, type());
EXPECT_CALL(mockD, getValue()).WillOnce(Return(0)); //not sure you want zero - use return value that you needs

然后-假设这是void codeUnderTest(IC& c)-您要测试的代码-只需将mockD传递给以下代码即可:
codeUnderTest(mockD);

如果出于某些其他原因同时需要MockDMockC,请使用模板定义来实现此目标:
template <typename Interface>
class MockCT : public Interface
{
public:
    MOCK_METHOD0(getValue, int());
};
using MockC = MockCT<IC>;

template <typename Interface>
class MockDT : public MockCT<Interface>
{
public:
    MOCK_METHOD0(type, void());
};
using MockD = MockDT<ID>;

09-05 19:57