chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes

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

asked by Ash (1.6k points)
edited by Ash

Please log in or register to answer this question.

...