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 thescratchpad
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>
, update theengine.load()
line to load the defaultmain.qml
if it doesn’t already and add a few lines to spin up and save a dummy Client object:
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));Client client;client.save();
Build...