You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I believe this is a bug. Originally posted on stackoverflow.
Note this seems very close to the issue 918, but I see that one is closed and I'm still seeing this issue with Moq version 4.14.0.0.
I've tried 3 different ways to setup a mock for a generic interface method. Only one way works, but it is using an explicit type, so won't work generically. I tried using It.IsAnyType, but it doesn't seem to match on the call that is made.
Here is the sample code (I expected test case 1 to have "asdf" returned). How can I get the mock to work for any type (not just string?)?
usingMoq;usingSystem;usingSystem.Threading.Tasks;namespaceblah{publicclassJunk:IGetOrAddable{publicstaticvoidMain(){Test(1);Test(2);Test(3);/*********OUTPUT********* For test case 1, value is null For test case 2, value is asdf Unhandled exception. System.ArgumentException: Object of type 'System.Func`2[System.Object,System.Threading.Tasks.Task`1[System.String]]' cannot be converted to type 'System.Func`2[System.Object,System.Threading.Tasks.Task`1[Moq.It+IsAnyType]]'. *************************/}publicstaticvoidTest(inttestCase){Mock<IGetOrAddable>mock=newMock<IGetOrAddable>();//setup the mock to always call the valueFactory function (ignore cache)switch(testCase){case1:{//use the It.IsAnyType to match any generic invocation of this methodmock.Setup(x =>x.GetOrAdd<It.IsAnyType>(It.IsAny<object>(),It.IsAny<Func<object,Task<It.IsAnyType>>>())).Returns((objectk,Func<object,Task<It.IsAnyType>>f)=>f(k));break;}case2:{//use an exact type (string?) to match a specific type invocation of this methodmock.Setup(x =>x.GetOrAdd<string?>(It.IsAny<object>(),It.IsAny<Func<object,Task<string?>>>())).Returns((objectk,Func<object,Task<string?>>f)=>f(k));break;}case3:{//try casting It.IsAny<object> per this suggestion: https://stackoverflow.com/a/61322568/352349mock.Setup(x =>x.GetOrAdd<It.IsAnyType>(It.IsAny<object>(),(Func<object,Task<It.IsAnyType>>)It.IsAny<object>())).Returns((objectk,Func<object,Task<It.IsAnyType>>f)=>f(k));break;}}varvalue=mock.Object.GetOrAdd<string?>(newobject(),RetrieveCoolValue).Result;Console.WriteLine($"For test case {testCase}, value is {value??"null"}");}publicTask<T>GetOrAdd<T>(objectkey,Func<object,Task<T>>valueFactory){//complicated cache retrieval stuff here of the sort described in https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd?view=netcore-3.1thrownewNotImplementedException();}publicstaticTask<string?>RetrieveCoolValue(objectkey){returnTask.FromResult<string?>("asdf");}}publicinterfaceIGetOrAddable{Task<T>GetOrAdd<T>(objectkey,Func<object,Task<T>>valueFactory);}}
The text was updated successfully, but these errors were encountered:
Pretty sure this is a duplicate of #919, given that your example uses It.IsAnyType as a nested part of another type (Task<It.IsAnyType>), support for which isn't implemented yet (like those other issues mention).
See the linked issues for suggestions how to work around this current limitation.
I believe this is a bug. Originally posted on stackoverflow.
Note this seems very close to the issue 918, but I see that one is closed and I'm still seeing this issue with Moq version 4.14.0.0.
I've tried 3 different ways to setup a mock for a generic interface method. Only one way works, but it is using an explicit type, so won't work generically. I tried using It.IsAnyType, but it doesn't seem to match on the call that is made.
Here is the sample code (I expected test case 1 to have "asdf" returned). How can I get the mock to work for any type (not just string?)?
The text was updated successfully, but these errors were encountered: