Results 1 to 1 of 1

Thread: UnitTests with mocked local objects

  1. #1
    Join Date
    Sep 2009
    Location
    Aachen, Germany
    Posts
    60
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default UnitTests with mocked local objects

    Hi everyone,

    for our UnitTests we use QTest and additionally Google Mock, but I struggle to decide how to write tests for methods which use local objects.

    Say I have this method:

    Qt Code:
    1. void foo::bar() {
    2. SomeObject object;
    3. object.load(member1);
    4. object.doSomething(member2, member3);
    5. useObject(object);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Now, to be able to test certain things I would like to use a MockSomeObject instead of SomeObject. One way to do that would look something like this:

    Qt Code:
    1. void foo::bar() {
    2. QScopedPointer<SomeObject> object(SomeObjectFactory::singletonPtr()->instance(member1));
    3. object->doSomething(member2, member3);
    4. useObject(object);
    5. }
    To copy to clipboard, switch view to plain text mode 

    In my test I would just replace SomeObjectFactory with MockSomeObjectFactory, which would create MockSomeObject.

    Another way would look like this:

    Qt Code:
    1. void foo::bar() {
    2. SomeObject object;
    3. object.load(member1);
    4. bar(object);
    5. }
    6.  
    7. void foo::bar(SomeObject& object) {
    8. object.doSomething(member2, member3);
    9. useObject(object);
    10. }
    To copy to clipboard, switch view to plain text mode 

    I discussed this with a colleague, and we would really appreciate other opinions or other possibilities.
    Last edited by ChiliPalmer; 27th March 2013 at 14:00.

Similar Threads

  1. Replies: 3
    Last Post: 9th January 2010, 16:47
  2. Qtestlib unittests & Qmake
    By saulit in forum Newbie
    Replies: 0
    Last Post: 5th February 2009, 20:06
  3. How to get and set local ip in windows xp using QT?
    By longtrue in forum Qt Programming
    Replies: 0
    Last Post: 5th May 2008, 09:55
  4. Replies: 7
    Last Post: 18th July 2006, 22:33
  5. How to get local IP
    By naresh in forum Qt Programming
    Replies: 11
    Last Post: 17th May 2006, 16:48

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
  •  
Qt is a trademark of The Qt Company.