A Mockpp Tutorial


Table of contents

The Problem

To demonstrate the various elements of mockpp a rather simple testing problem will be solved in different ways. There is a class Consumer which loads a configuration file. This file is a simple text file with a data record in each line. Each line will be processed and written back in a modified form at the end. For simplicity the string "/processed" is appended to the original record. While processing the data a method calculate is invoked with not exactly defined values.

Consumer uses another class derived from the pure virtual class Interface which provides methods for opening, reading and writing the data file:

class Interface
  virtual void open(const std::string &name) = 0;
  virtual std::string read() = 0;
  virtual unsigned calculate(unsigned input) = 0;
  virtual void write(const std::string &data) = 0;
  virtual void close() = 0;

This is a rather common testing problem: there are two or more classes that depend on each other. When you want to test Consumer from this example you should pass it an object with predictible behavour, a so-called mock object. Creating an input file in this case and checking the output manually after every change in the sources would become too boring.

A mock object is totally different from the object used in production code and only used for testing. Ideally it is derived from the same base class as the production code.

The following sections will introduce into the most important elements of mockpp by developing different solutions.

All files are present as working source code to start playing with them under mockpp/examples/tutorial .

Next: Solution 1: Basic Mock Objects

Table of contents

 All Classes Namespaces Files Functions Variables Typedefs Friends Defines

Generated on Tue Jan 5 18:03:32 2010 for mockpp-tutorial by  doxygen 1.6.1