I have a class Base that has several children, say A, B, C. For testing purposes I’d like to mock those derived classes by deriving from them. So MockA derives from A, MockB derives from B and so on.
The problem is, that MockA, MockB, … all inherit protected members from Base that have to be set in the same way in each mock class. The code looks like this:
MockA::init()
{
x = 1; // inherited from Base
y = "abc"; // inherited from Base
z = 0.5; // inherited from Base
}
MockB::init()
{
x = 1; // inherited from Base
y = "abc"; // inherited from Base
z = 0.5; // inherited from Base
}
and so on.
So my question is, how can I avoid this cut&paste initialization? Can I achieve it without touching Base, A, B, C, …?
You want a “mixin”. In C++ they are usually implemented with templates and specifically using CRTP, the “curiously recurring template pattern”. It might be an overkill if it’s just a bit of common code, than Pierre’s answer seems most appropriate. But as the amount of common code for the mock classes grows, so will value of template.
Basic CRTP would go like this:
template <typename ConcreteT> class Mock {
initMock() {
static_cast<ConcreteT *>(this)->x = 1;
static_cast<ConcreteT *>(this)->y = "abc";
static_cast<ConcreteT *>(this)->z = 0.5;
}
};
class MockA : A, Mock<MockA> { ... };
// ^^^^^ specialized on the type that inherits it, that's the point
Here it would make things simple to inherit A through the Mock class though, so
template <typename BaseT> class Mock : public BaseT {
initMock() {
x = 1;
y = "abc";
z = 0.5;
}
};
class MockA : Mock<A> { ... };
// ^ just the base class
That does away with the ugly static_casts, but does not allow you to provide methods in MockA
that will be called by Mock
. But you can have both…
template<typename ConcreteT, typename BaseT>
class Mock : public BaseT { ... };
class MockA : Mock<MockA, A> { ... };
Now Mock
can call methods of MockA
on itself via static cast and call methods of A
(or rather the ultimate base) directly.
Note: The mixin templates are basically the same thing as mixins in Ruby or interfaces with method implementations introduced in Java 8. Those languages it is a special construct, because they don’t have anything of the power of C++ templates, but the implementation is similar—an intermediate class is constructed by compiler and injected in the hierarchy.
4
Well, you could introduce a static method which returns a default set of all protected member values and fetch them in each init method into the fields of the mock objects.
Edit:
MockA::init()
{
initMock(MockInitializer.getDefaultValues());
//go on with your code
}
MockA::initMock(anystruct def_values)
{
//assign your values
}
The problem is that in any place you’ll have to do the assignment, why not do it in its own method?
5
Here is how i would to that if I understand your problem :
class Base
{
protected:
initMock()
{
x = 1;
y = "abc";
z = 0.5;
}
};
class A { /* Whatever you want ... */ };
class B { /* Whatever you want ... */ };
class MockA : public A, public Base
{
public:
initMock()
{
Base::initMock();
}
};
And you can do the same thing with MockB.
10
If Base
is the common base class of A, B, C holding protected members Base::x
, Base::y
, etc., then I guess there are accessor methods like
Base::SetX(type_x xval){x=_val;}
etc. So write a static function in a helper class MockInit
void MockInit::initForTests(Base &b)
{
b.SetX(1);
b.SetY("abc");
b.SetZ(0.5);
}
and call that function this way:
MockA::init()
{
MockInit::initForTests(*this);
}
MockB::init()
{
MockInit::initForTests(*this);
}
That’s it – simple and stupid. However, if there are no such accessor methods, and you have you have absolutely no access to x,y,z from outside the inheritance hierarchy, I would consider changing the initial design of Base
for better testability. At least, I would consider to make MockInit
a friend class of Base
.
4