Hello,
I am working on a C++ unit test. I have a production code like below.
I am having trouble for redirecting a method call with a derived class which inherits from a base class as protected.
I want to call MockMethodA on anotherObj. (anotherObj is an instance of a concrete class in my unit test project) when methodA is called inside methodB.
But It does not work. It calls methodA of BaseClass. I tried both WHEN_CALLED and PRIVATE_WHEN_CALLED macros.
WHEN_CALLED(d->methodA()).DoMemberFunctionInstead(&anotherObj, MockMethodA);
PRIVATE_WHEN_CALLED(d, methodA).DoMemberFunctionInstead(&anotherObj, MockMethodA);
d->methodB();
//production code. It cannot be changed.
class BaseClass
{
public:
BaseClass(){};
virtual ~BaseClass(){};
virtual void methodA()
{
cout << \"methodA\" << endl;
}
};
class DerivedClass :protected BaseClass
{
public:
DerivedClass(){};
~DerivedClass(){};
void methodB()
{
cout << \"methodB\" << endl;
BaseClass::methodA();
}
};