eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.24k stars 170 forks source link

No way to mock a QObject based interface? #104

Open snowpong opened 7 years ago

snowpong commented 7 years ago

Hi,

With Google Mock we declare the actual mock class ourselves, and are thus able to add f.ex the Q_OBJECT macro. See an example of a QObject interface mocked with Google Mock here . Because we can add the Q_OBJECT macro, qmake and moc generate the extra code needed for Qt's meta-object system.

Is there a way that we can add Q_OBJECT to the mock class with FakeIt, so that qmake and moc can do their job?

regards, Espen

ColinDuquesnoy commented 7 years ago

I have the same question too.

sandboxcoder commented 5 years ago

I've just ran into this issue at my job as well. I was using FakeIt and it seemed successful at first. But once my Mock object hits the Qt ecosystem (Qt framework code) it appears to blow up in the debugger (getting Bad Access error). It quickly dawned on me this is due to the Mock object not using the Q_OBJECT macro.

Iqon commented 4 years ago

FYI: we thought we had the same problem as well, but I did a small test and mocking QObject's did work fine for me.

Filename: "FakeQObject.cpp"

class AQtClassToBeMocked : public QObject
{
Q_OBJECT

public:
    virtual int aFunction()
    {
        return 2;
    }
};

TEST_CASE("Working example with QObject")
{
    fakeit::Mock<AQtClassToBeMocked> theQtStub;
    fakeit::When(Method(theQtStub, aFunction)).Return(1);

    CHECK(1 == theQtStub().aFunction());
}

#include "FakeQObject.moc"

This compiles and runs without any exceptions. So you could derive from the Interface, which adds the Q_OBJECT and use this in your tests.