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
0 votes

I think that this may be a known issue but I haven't seen any posts or documentation that presents a workaround.

The following code is a simple example that illustrates the problem that I am having while trying to test overloaded generic methods.

    public interface ITest { }

    public class Foo { }

    public class MyCUT
        public Foo Send(Foo foo)
            return null;

        public T Send<T>(string name, T obj) where T : ITest
            return default(T);

    public class MyTestFixture
        public void Send()
            MyCUT mc = Isolate.Fake.Instance<MyCUT>();

            ITest test = Isolate.Fake.Instance<ITest>();
            Isolate.WhenCalled(() => mc.Send<ITest>(default(string), test)).WillReturn(null);
            mc.Send<ITest>("test", test);

If I run the above test I get the following exception:

failed: System.InvalidCastException : Unable to cast object of type 'UnitTests.Foo' to type 'UnitTests.ITest'.
C:UnitTestsTestFixture.cs(21,0): at UnitTests.MyCUT.Send[T](String name, T obj)
C:UnitTestsTestFixture.cs(36,0): at UnitTests.MyTestFixture.<>c__DisplayClass1.<Send>b__0()
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
C:UnitTestsTestFixture.cs(0,0): at UnitTests.MyTestFixture.Send()

If this is a known defect or limitation please provided me with a workaround. If I am just doing something wrong (I hope) then please point that out.


asked by Darin_Creason (3.5k points)

1 Answer

0 votes
Hi Darin,

You seem to have found a bug :( The combination of recursive fake and the generic method with the same name and different arguments threw us off here. A workaround would be using a different fake type. Creating your fake with the Members.CallOriginal default behavior passes your test:
MyCUT mc = Isolate.Fake.Instance<MyCUT>(Members.CallOriginal);

I am sorry about the inconvenience - I will work to patch this bug in an upcoming version.

Typemock Support
answered by doron (17.2k points)