Results 1 to 4 of 4

Thread: Message as QReturnArgument?

  1. #1
    Join Date
    Jun 2013
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Message as QReturnArgument?

    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:

    Qt Code:
    1. TheClassToTest * test = new TheClassToTest();
    2.  
    3. 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):

    Qt Code:
    1. 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:

    Qt Code:
    1. TheClassToTest * test = new TheClassToTest();
    2. QMessageBox returnVal;
    3. QString result = "You're logged in now!"
    4.  
    5. QMetaObject::invokeMethod(test, "login", Qt::DirectConnection, Q_RETURN_ARG(QMessageBox, returnVal), Q_ARG(QString, username) , Q_ARG(QString, password));
    6.  
    7. 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:

    Qt Code:
    1. Actual (returnVal.text()):
    2. 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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A Golf Swing Drill To Help You Stay Connected

    I don't see a reason why you want to use invokeMethod() here.
    Simply call the login slot directly.

    As for the message, use a QSignalSpy to connect to the QStatusBar's messageChanged() signal and then compare the value from the spy.

    Something like

    Qt Code:
    1. QSignalSpy messageSpy(statusBar, SIGNAL(messageChanged(QString)));
    2.  
    3. // perform login
    4.  
    5. QCOMPARE(messageSpy.count(), 1); // expect one emit
    6. QCOMPARE(messageSpy[0].count(), 1); // signal has one argument
    7. QCOMPARE(messageSpy[0][0].toString(), expectedMessage);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Jun 2013
    Posts
    10
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A Golf Swing Drill To Help You Stay Connected

    Quote Originally Posted by anda_skoa View Post
    I don't see a reason why you want to use invokeMethod() here.
    Simply call the login slot directly.

    As for the message, use a QSignalSpy to connect to the QStatusBar's messageChanged() signal and then compare the value from the spy.

    Something like

    Qt Code:
    1. QSignalSpy messageSpy(statusBar, SIGNAL(messageChanged(QString)));
    2.  
    3. // perform login
    4.  
    5. QCOMPARE(messageSpy.count(), 1); // expect one emit
    6. QCOMPARE(messageSpy[0].count(), 1); // signal has one argument
    7. QCOMPARE(messageSpy[0][0].toString(), expectedMessage);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Thank you for your answer, I will look into the QSignalSpy. I tried to call the method directly but can't, since it is a private slot. Me googeling how to test a private method resulted in using invokeMethod().

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A Golf Swing Drill To Help You Stay Connected

    Quote Originally Posted by QphiuchuS View Post
    I tried to call the method directly but can't, since it is a private slot. Me googeling how to test a private method resulted in using invokeMethod().
    Ah, yes, I see.
    Ideally this wouldn't be necessary, i.e. if a unit's interface it difficult to test, then the interface is most likely not they way it should be.
    But if there is no way around such a restriction at the given time, then things like that can indeed be helpful

    Another thing: since login procedure are often asynchronous, a test will have to wait while keeping the event loop running. That can be done with either QTest::qWait() or a local event loop that exits on receiving the target signal.

    Cheers,
    _

Similar Threads

  1. error message
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2009, 14:08
  2. Replies: 4
    Last Post: 12th October 2008, 13:47
  3. Get Windows message
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2008, 10:28
  4. what does these debug message mean?
    By wesley in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 27th February 2008, 07:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.