Search code examples
c#genericsnhibernatejustmock

Preferring non-generic version of method over generic version in c#


I'm trying to stub an NHibernate ICriteria using JustMock.

Specifically I'm trying to stub the List<> method when it is called with an array of object:

var mockCriteria = Mock.Create<ICriteria>();
Mock.Arrange(() => mockCriteria.List<object[]>()).Returns(
    new object[]
    {
        new object[] {"CompanyX", 1, 1, 1, 0}, 
        new object[] {"CompanyX", 1, 1, 1, 0}, 
        new object[] {"CompanyY", 2, 1, 1, 0}
    });

When I execute the second line (the Arrange) I'm getting the error:

System.InvalidOperationExceptionSystem.Collections.IList List() is not a GenericMethodDefinition. MakeGenericMethod may only be called on a method for which MethodBase.IsGenericMethodDefinition is true.
   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at \x7\x5\x2.\x8\x2.\x3\x15\x2(Type \x8, MethodBase , BindingFlags \x1C\xF\x2)   at \x6\x2.\x6\x2.Create(Object , MethodInfo \x6, Boolean \x6\x2)   at \x7\x5\x2.\x11\x14\x2.\x11\x13\x2(\xF\x2 \x5\xF)   at Telerik.JustMock.Mock.\x1F.\x15\x2(\x11\x14\x2 \x2)   at \x7\x5\x2.\x5\x14\x2.\x1C[\x4\x14\x2,\x5\x2](\xF\x2 \xF\x2, Func`2 \x3\x14\x2)   atThreshold.DeviceManagerGateway.UnitTests.Queries.DeviceNetworkStatusQueryFacts.RetrieveDevicesAsDeviceNetworkStats() in DeviceNetworkStatusQueryFacts.cs: line 24

ICriteria has both a List() and a List<T>() method and it looks like the compiler is picking up the non-generic version of the List method instead of the generic version. Assuming that I'm correct and that it's picking up the wrong version of the List method, does anybody know how to force this? Or if it's a different problem can anyone point me to how to solve this?


Solution

  • I tried it with a recent version of JustMock and the problem doesn't crop up.

    Judging by the presence of obfuscation, it seems that you're using a pretty old version.

    You can download the latest version of JustMock from NuGet or from your Telerik account.