Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

test.cpp

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

DependsOnUgly.hpp

#ifndef _DEPENDS_ON_UGLY_HPP_
#define _DEPENDS_ON_UGLY_HPP_
#include <string>
#include "Ugly.hpp"
class DependsOnUgly {
public:
    std::string getDescription() {
        return "Depends on " + Ugly().getName();
    }
};
#endif

Ugly.hpp

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

NotAsUgly.hpp

#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
};
#endif

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.

share|improve this question
    
I posted about this technique, and a closely related one, on StackOverflow. –  Kazark Apr 15 '13 at 20:14
add comment

3 Answers

"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.

share|improve this answer
add comment
up vote 2 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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