Using Moq for Common Methods

Completely simple situation, but I can not get it to work. I am faced with the problem of using Moq to bully the general method (in this case, in the Ninject Kernel interface):

T Get<T>(); 

I set my object layout:

 Mock<IKernel> mockKernel = new Mock<IKernel>(); mockKernel.Setup(x => x.Get<IGetUserQuery>()).Returns(new GetUserQuery()); 

At runtime, I get the following exception:

 Expression references a method that does not belong to the mocked object: x => x.Get<IGetUserQuery>(new[] { }) 

Any idea why this is throwing this? I mocked generics in Moq before without any problems ... Are there cases where general perplexity is not supported? This seems like a simple case. The only wrinkle is that IGetUserQuery, in turn, inherits from a generic type:

 IGetUserQuery : ICommand<UserQueryInput, UserQueryOutput> 

I don’t see this creating a problem because the generic types for this ICommand implementation are statically defined by IGetUserQuery, so I doubt it confuses Moq.

Thanks in advance

+6
source share
1 answer

The problem is that T Get<T> () is not really a method defined in the IKernel interface, it is an extension method defined here .

Why are you trying to make fun of T Get<T> () in the first place? Interaction with the IoC container should be absolutely minimal, as a rule, at the access point "entry point" into your system.

+5
source

Source: https://habr.com/ru/post/892496/


All Articles