Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPP FakeIt library multiple inheritence

I'm comparing GoogleMock vs FakeIt for writing unit tests. 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.

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. 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?

like image 274
broun Avatar asked Sep 27 '22 06:09

broun


1 Answers

Unfortunately it seems that any type of multiple inheritance is not supported, even if it's just an "interface" that unifies several other "interfaces", e.g.:

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>)

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!
like image 91
Michael Kuritzky Avatar answered Sep 30 '22 07:09

Michael Kuritzky