ninject / Ninject

the ninja of .net dependency injectors
http://ninject.org/
Other
2.68k stars 530 forks source link

Latest Ninject release breaks MoqMockingKernel #392

Closed simongh closed 2 years ago

simongh commented 2 years ago

The latest release of Ninject (3.3.5) causes a breaking change in the MoqMockingKernel.

For given binding such as: kernel.Bind<IService>().To<MyService> Ninject will return an instance of MyService when IService is requested. If we request IEnumerable<IService> Ninject will return a collection containing MyService

In unit tests where we're using MoqMockingKernel, we might set up a mock for IService as

var svc = kernel.GetMock<IService>();
svc.Setup(s => s.Name).Returns("found");

If we use the MoqMockingKernel to request IService we get a mock instance. If we request IEnumerable<IService> we get instead an empty enumerable, not as expected the collection of mocks.

This works as expected when 3.3.4 is used.

simongh commented 2 years ago

ConsoleApp1.zip

Code sample showing the behaviour

scott-xu commented 2 years ago

Thanks @simongh . It is considered as a bug fix. See https://github.com/ninject/Ninject/issues/263.

scott-xu commented 2 years ago

You need to explicitly bind IEnumerable<IService> to get your mocked services