2 added 325 characters in body
source | link

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.

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);
}

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.

1
source | link

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);
}