Hi there,
I've recently started using Typemock in an attempt to demo it at our company. I've read the documentation 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.
}
}
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