I am dealing with some old, sloppy C++ code in which there is a structure with a lot of data members and functions. I want to test a class that I have implemented which uses the struct, but this dependency is going to make the unit tests extremely ugly. Inspired by Michael Feather's discussion of preprocessing seams in Working Effectively with Legacy Code, I am getting ready to solve this problem by faking the ugly structure with the preprocessor (not with inheritance) polymorphism as demonstrated in the following notional code.


#include <iostream>
#include "NotAsUgly.hpp"
#include "DependsOnUgly.hpp"
int main() {
    std::cout << DependsOnUgly().getDescription() << std::endl;


#include <string>
#include "Ugly.hpp"
class DependsOnUgly {
    std::string getDescription() {
        return "Depends on " + Ugly().getName();


#ifndef _UGLY_HPP_
#define _UGLY_HPP_
struct Ugly {
    double a, b, ..., z;
    void extraneousFunction { ... }
    std::string getName() { return "Ugly"; }


#ifndef _UGLY_HPP_ // Same name as in Ugly.hpp---deliberately!
#define _UGLY_HPP_
struct Ugly { // Once again, duplicate name is deliberate
    std::string getName() { return "not as ugly"; } // All that DependsOnUgly depends on

Is there a special name for the type of fake object I'm using here? I'm inclined to call it a stub, but it is rather different than what I would normally think of as a stub. I would expect something called a stub to implement the interface of the class which is to be faked through inheritance. Here the implementation is only partial, and happens through the preprocessor.

Is there already a known name for this kind of fake/testing object? I want to name the header file that contains it (NotAsUgly.hpp in my example) to properly signify to other developers what it is for.


"Mock Object" is a common term.

Fake objects commonly provide a set of method stubs, returning pre-set values.

Mock objects commonly are richer in their behaviour, mimicking a more complete set of the real object behaviour.


This question has gone almost two months without anything but generic answers, so I judge that this is not a well-known pattern with an existing name. Therefore I suggest the name Preprocessor Partial Mock, to indicate that it is injected via a preprocessor seam and is only a partial implementation of the class being mocked.


You could use the generic term "Fake" here.
On the other hand, "Stub" is also a good name. Your description of the fake class fits nicely with the definition of stub on wikipedia.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.