I have the following simple classes:
public class ClassA
{
}
public class ClassB
{
}
public static class GenericClass<X> where Y : IList<X>, new()
{
public static Y GetCollection(X x)
{
var y = new Y();
y.Add(x);
return y;
}
}
public static class NonGenericClass
{
public static Y GetCollection<X>(X x) where Y : IList<X>, new()
{
var y = new Y();
y.Add(x);
return y;
}
}
and two tests:
[Test, Isolated]
public void Test1()
{
var fakeA = new List<ClassA>();
Isolate.WhenCalled(() => GenericClass<ClassA, List<ClassA>>.GetCollection(null)).WillReturn(fakeA);
var fakeB = new List<ClassB>();
Isolate.WhenCalled(() => GenericClass<ClassB, List<ClassB>>.GetCollection(null)).WillReturn(fakeB);
}
[Test, Isolated]
public void Test2()
{
var fakeA = new List<ClassA>();
Isolate.WhenCalled(() => NonGenericClass.GetCollection<ClassA, List<ClassA>>(null)).WillReturn(fakeA);
var fakeB = new List<ClassB>();
Isolate.WhenCalled(() => NonGenericClass.GetCollection<ClassB, List<ClassB>>(null)).WillReturn(fakeB);
}
Both of them fail with the following error:
System.InvalidCastException : Unable to cast object of type 'System.Collections.Generic.List`1[Lfx.Global.Tests.Unit.ClassA]' to type 'System.Collections.Generic.List`1[Lfx.Global.Tests.Unit.ClassB]'.
They fail on the second WhenCalled statement and only if both WhenCalled statements included. It's no problem to fake each of them separately.
Looks like Isolator gets somewhat gets confused by methods' signatures and tries to use first fake in the second statement causing invalid cast exception.
I have more complex code where this happens, I made this trivial example to show simplify reproducing of this bug.
Good luck!