Skip to content
Adrian Edwards edited this page Oct 16, 2017 · 7 revisions

Introduction

This extension provides integration between Ninject and Moq, creating a "lightweight" auto-mocking container. In your tests, you should use the MockingKernel instead of the StandardKernel and the Module class corresponding to your mocking framework instead of the NinjectKernel.
It adds the following features to Ninject:

  1. The following syntax will bind a service to the mocked object of a Mock: Bind().ToMock(); This binding is only necessary if you want to refine your binding with the returned IBindingWhenInNamedWithOrOnSyntax<>.

  2. If you request a service that has no binding, instead of creating an implicit self-binding, the MockingKernel will create an instance of Mock and return the mocked object associated with it.

  3. The MockingKernel has a new GetMock() method to get the real mock object. You can use this to do your mock setup and verification.

  4. 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.

Update to a newer release of the mocking framework

To use Ninject.MockingKernel with the latest mocking framework of your choice you just have to update the mocking framework in your project. E.g. for Moq execute the following command in the Nuget Package Manager Console (see also https://github.com/ninject/ninject.mockingkernel/pull/15#issuecomment-37828648)

Update-Package Moq

Binaries