I'm trying to unit test some legacy code which is using QTimer for some periodic action basically:
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject* parent = nullptr) : QObject(parent)
{
timer.setInterval(1000);
timer.setSingleShot(false);
timer.start();
connect(&timer, &QTimer::timeout, this, &MyClass::onTimeout);
}
int getValue() const
{
return value;
}
signals:
public slots:
void onTimeout()
{
value++;
}
private:
int value{0};
QTimer timer{this};
};
I need to write unit test for the method onTimeout() hence I'm trying to disconnect the QTimer::timeout from MyClass::onTimeout and trigger the update from the unit test.
void testFirstUpdate()
{
MyClass myClass;
qDebug() << "disconnect: " << myClass.disconnect(); //returns false
myClass.dumpObjectInfo();
QCOMPARE(myClass.getValue(), 0);
myClass.onTimeout();
QCOMPARE(myClass.getValue(), 1);
}
How can I make the disconnect working? Or is there a better way controlling QTimer from QtTestLib? Is adding MyClass::disconnectTimer() the only viable option?