Hi!
I'm supposed to test an application with QTest. Now I would like to test a login function, which is void, but returns (I suppose) QMessages (I'm not sure though). Ok, I call the function from my test class:
TheClassToTest * test = new TheClassToTest();
TheClassToTest * test = new TheClassToTest();
QMetaObject::invokeMethod(test, "login", Qt::DirectConnection, Q_ARG(QString, username) , Q_ARG(QString, password));
To copy to clipboard, switch view to plain text mode
This works, but obviously I am not comparing anything yet. If the login attempt is successful, the ClassToTest shows a Message (and this message in fact appears when i call my test class):
statusBar()->showMessage("You're logged in now!", 4000);
statusBar()->showMessage("You're logged in now!", 4000);
To copy to clipboard, switch view to plain text mode
Since this function is void, this is the only reaction.
Now I would like to somehow get the content of this message an compare it to a predefined string, to see, if the login was indeed successful. In a very naive attempt I tried it this way:
TheClassToTest * test = new TheClassToTest();
QString result
= "You're logged in now!"
QCOMPARE(returnVal.text(), result);
TheClassToTest * test = new TheClassToTest();
QMessageBox returnVal;
QString result = "You're logged in now!"
QMetaObject::invokeMethod(test, "login", Qt::DirectConnection, Q_RETURN_ARG(QMessageBox, returnVal), Q_ARG(QString, username) , Q_ARG(QString, password));
QCOMPARE(returnVal.text(), result);
To copy to clipboard, switch view to plain text mode
The MessageBox no longer appears if I now run the test, but the test still fails since the string I try to retrieve from my returnVal is empty:
Actual (returnVal.text()):
Expected (result): You're logged in now!
Actual (returnVal.text()):
Expected (result): You're logged in now!
To copy to clipboard, switch view to plain text mode
I could not find how QStatusBar::showMessage() is implemented, maybe the error is that showMessage does not use a QMessageBox?
I would really appreciate some help.
Thank you.
Bookmarks