简体   繁体   English

CPP FakeIt 库多重继承

[英]CPP FakeIt library multiple inheritence

I'm comparing GoogleMock vs FakeIt for writing unit tests.我正在比较 GoogleMock 和 FakeIt 编写单元测试。 I like FakeIt over GoogleMock because I'm from Java background and FakeIt sticks close to Mockito/JMock syntax which make using the library much easier to write & maintain.我喜欢 FakeIt 而不是 GoogleMock,因为我来自 Java 背景并且 FakeIt 坚持接近 Mockito/JMock 语法,这使得使用库更容易编写和维护。

But FakeIt GIT home ( https://github.com/eranpeer/FakeIt ) says it doesn't support MultipleInheritance and the application im testing has code with multiple inheritance.但是 FakeIt GIT 主页 ( https://github.com/eranpeer/FakeIt ) 说它不支持 MultipleInheritance 并且我测试的应用程序具有多重继承的代码。 I dont have to support diamond inheritance, so I would like to know if its just that aspect of multiple inheritance thats not supported or are there other aspects thats not supported as well?我不必支持钻石继承,所以我想知道它是否只是不支持多重继承的那个方面,或者是否还有其他方面不支持?

Unfortunately it seems that any type of multiple inheritance is not supported, even if it's just an "interface" that unifies several other "interfaces", eg:不幸的是,似乎不支持任何类型的多重继承,即使它只是一个统一其他几个“接口”的“接口”,例如:

struct IA { virtual void a() = 0; };
struct IB { virtual void b() = 0; };
struct IC : public IA, public IB {};
fakeit::Mock<IC> mock; // error :(

(The check is done using std::is_simple_inheritance_layout<T> ) (检查是使用std::is_simple_inheritance_layout<T>

I did, however, find a little workaround for this problem, at least for simple scenarios:但是,我确实为这个问题找到了一些解决方法,至少对于简单的场景:

class MockC : public IC {
public:
    MockC(IA& a, IB& b) : m_a(a), m_b(b) {}
    void a() override { return m_a.a(); };
    void b() override { return m_b.b(); };
private:
    IA& m_a;
    IB& m_b;
};

fakeit::Mock<IA> mockA;
fakeit::Mock<IB> mockB;
MockC mockC(mockA.get(), mockB.get());
// Use mockA and mockB to set up the mock behavior the way you want it.
// Just make sure not to use mockC after they go out of scope!

Here's another workaround that doesn't require you to make a special mock class.这是另一种不需要您制作特殊模拟课程的解决方法。 All you need to do is to mock each of the base classes, and apply it to an instance of the deriving class by casting the deriving class to a reference of each interface.您需要做的就是模拟每个基类,并通过将派生类转换为每个接口的引用来将其应用于派生类的实例。 You do need to apply the mock to an instance of the class.您确实需要将模拟应用于类的实例。 Here is an example:下面是一个例子:

class I1
{
public:
    virtual int one() = 0;
};

class I2
{
public:
    virtual int two() = 0;
};

class Both : public I1, public I2
{
public:
    virtual int one()
    {
        return 0;
    }
    virtual int two()
    {
        return 0;
    }
    virtual int three()
    {
        return one() + two();
    }
};

We have pure interfaces I1 and I2 with pure virtual methods one() and two() respectively, all implemented by Both .我们有纯接口I1I2带有纯虚方法one()two() ,它们都由Both实现。 As you might guess, Both is deliberately designed to produce an incorrect answer to demonstrate the mock.正如您可能猜到的那样, Both是故意设计来产生错误答案来演示模拟的。 Here is the mock inside a Google Test test:这是 Google Test 测试中的模拟:

TEST(both_mock, three)
{
    Both both;
    Mock<I1> mock1((I1&)both);
    Mock<I2> mock2((I2&)both);

    When(Method(mock1, one)).Return(1);
    When(Method(mock2, two)).Return(2);

    ASSERT_EQ(both.three(), 3);
}

And this works and passes.这有效并通过。 The advantage of this is that you don't need to create a special mock class, and you can use the actual class that inherits multiple classes.这样做的好处是不需要创建专门的mock类,可以使用继承多个类的实际类。 The disadvantages are...缺点是...

  • The deriving class ( both in this case) must be instantiable (eg, you can't do this with an abstract class or interface that inherits from other abstract classes or interfaces).在派生类( both在这种情况下)必须实例化(例如,你不能用一个抽象类或接口做到这一点,从其他抽象类或接口继承)。
  • If you further subclass the subclass of both interfaces (eg, class More : public Both ), you still need one mock for each interface/base class, and you cannot mock any member declared by Both , More , or any further deriving class.如果您进一步子类化两个接口的子类(例如, class More : public Both ),您仍然需要为每个接口/基类模拟一个,并且不能模拟由BothMore或任何进一步派生类声明的任何成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM