Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Getting generic type from a faked generic method

+1 vote


I want to fake a generic method like below:

public void foo<T>(Object params) where T : SomeType {}

I want to be able to check the actual type for T in my faked method. Is this possible?

asked Sep 5, 2017 by Ash (180 points)

Hi Ash,

I didn't quite understand what is it that you want to test, but if you want to verify that the method was called you can try:

Isolate.Verify.WasCalledWithAnyArguments(() => fake.foo<SomeType>());

If that's not what you meant, please send a simple code sample.

1 Answer

+1 vote

You can try the following and assert against it:

var c = new SomeClassA();
bool wasCalledWithType = false;
Isolate.WhenCalled(() => c.foo<SomeClassB>(null)).DoInstead(context =>
      if (context.Method.GetGenericArguments() is SomeClassB)
           wasCalledWithType = true;




answered Oct 24, 2017 by Raphy (2,930 points)