I've recently started using Typemock and have looked through at least half a dozen existing threads, but can't seem to find an answer to my exact problem.
I have a class C that derives from B, that itself derives from A. The instance of C is what I need to test and therefore needs to be real. But as C derives from B (and indirectly A), I need those two classes completely faked automatically when I create an instance of C.
See below:
class A
{
protected int methodA()
{
return 1;
}
}
class B : A
{
protected int methodB()
{
return 5;
}
}
class C : B
{
public int methodC()
{
return methodA() * methodB();
}
}
class Test
{
public Test()
{
A fake_A = Isolate.Fake.AllInstances<A>(Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored);
Isolate.NonPublic.WhenCalled(fake_A, "methodA").DoInstead((MethodCallContext ctx) =>
{
return 2;
}
);
B fake_B = Isolate.Fake.AllInstances<B>(Members.ReturnRecursiveFakes, ConstructorWillBe.Ignored);
Isolate.NonPublic.WhenCalled(fake_B, "methodB").DoInstead((MethodCallContext ctx) =>
{
return 10;
}
);
C c = new C(); // Would expect A and B to be faked automatically since I've faked all instances above.
int method_c_val = c.methodC(); // I get back 5, but what I need returned is 20.
}
}
This is an extremely simplified version of the problem I'm facing. It's not just a matter of faking behaviour of one or two methods in the parent classes. The entire parent heirarchy needs to be faked. So A and B both need to be faked completely, not partially.
One of the threads I've already looked at is this one, but the answer in there points to an address which is no longer available:
https://www.typemock.com/answers/11613/mocking-all-instances-of-a-base-class
As @Nkosi suggested you can use:
You fake the C object but it's methods still call the original implementation (If necessary), and you fake only the methods in question.