I'm trying to write an unit test for a class in a Qt-based project (Qt 5, C++03).
class Transaction { // This is just a sample class
//..
public signals:
void succeeded();
void failed();
}
Transaction* transaction = new Transaction(this);
QSignalSpy spy(transaction, SIGNAL(succeeded()));
transaction->run();
spy.wait(5000); // wait for 5 seconds
I want my test to run faster.
How to interrupt this wait()
call after signal failed()
is emitted, in case of failed transaction?
I do not see any slots available in the QSignalSpy class.
I should use QEventLoop instead?
You'll probably have to use a loop and manually call QTest::qWait()
while neither signal has been emitted:
QSignalSpy succeededSpy(transaction, SIGNAL(succeeded()));
QSignalSpy failedSpy(transaction, SIGNAL(failed()));
for (int waitDelay = 5000; waitDelay > 0 && succeededSpy.count() == 0 && failedSpy.count() == 0; waitDelay -= 100) {
QTest::qWait(100);
}
QCOMPARE(succeededSpy.count(), 1);
Solution with QTestEventLoop
:
QTestEventLoop loop;
QObject::connect(transaction, SIGNAL(succeeded()), &loop, SLOT(exitLoop()));
QObject::connect(transaction, SIGNAL(failed()), &loop, SLOT(exitLoop()));
transaction->run();
loop.enterLoopMSecs(3000);
Solution with a timer and QEventLoop
:
Transaction* transaction = new Transaction(this);
QSignalSpy spy(transaction, SIGNAL(succeeded()));
QEventLoop loop;
QTimer timer;
QObject::connect(transaction, SIGNAL(succeeded()), &loop, SLOT(quit()));
QObject::connect(transaction, SIGNAL(failed()), &loop, SLOT(quit()));
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(3000);
loop.exec();
transaction->run();
QCOMPARE(spy.count(), 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With