Mocking

The unit tests we’ve written so far have all been pretty straightforward. While our Client class isn’t totally independent, its dependencies are all other data models and decorators that it can own and change at will. However, looking forward, we will want to persist client data in a database. Let's look at a few examples of how this can work and discuss how the design decisions we make impact the testability of the Client class.

Open up the scratchpad project and create a new header mocking.h file, where we’ll implement a dummy Client class to play around with.

mocking.h:

#ifndef MOCKING_H
#define MOCKING_H
#include <QDebug>
class Client
{
public:
    void save()
    {
        qDebug() << "Saving Client";
    }
};
#endif

In main.cpp, #include <mocking.h> ...

Get End to End GUI Development with Qt5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.