This extension provides integration between Ninject and Moq/NSubstitute/RhinoMocks/FakeItEasy, creating a "lightweight" auto-mocking container.
In your tests, you should use the MockingKernel instead of the StandardKernel. It adds the following features to Ninject:
- The following syntax will bind a service to the mocked object of a
Mock<T>
.
Bind<IService>().ToMock();
You can add additional interfaces to the mock:
Bind<IService>().ToMock(typeof(IInterface1), typeof(IInterface2));
-
If you request a service that has no binding, instead of creating an implicit self-binding, the MockingKernel will create an instance of
Mock<T>
and return the mocked object associated with it. -
A
Reset()
method is available, which clears the Ninject cache of any activated instances, regardless of whether they remain in scope. You can call this method after each test to ensure that instances are reactivated, without having to dispose and re-initialize the kernel each time.
Hat tip to Scott Reynolds for the idea, and to Sean Chambers for dogfooding.
https://github.com/ninject/Ninject.MockingKernel/wiki