Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Moq provider for Ninject?

I want to create a simple Ninject provider that returns Moq'd instances instead of concrete types. So far I have this:

public class NinjectMockProvider<T> : IProvider
{
    public static Type Type { get { return typeof(T); } }

    public object Create(IContext context)
    {
        Mock<T> newMock = new Mock<T>();
        return newMock.Object;
    }
}

But this is all wrong I'm sure as I don't know what I'm doing really. Any help and code samples would be great. I just want the ability to do:

kernel.Bind<ISomeInterface>().ToProvider<NinjectMoqProvider<ISomeInterface>>();

or something to that effect.

Update

I did figure out that I could accomplish what I want by using Ninject's method binding:

kernel.Bind<ISomeInterface>().ToMethod(x => new Mock<ISomeInterface>().Object);

I still would like a more elegant way and I may have to check out Ninject.Moq as suggested by Ian, but if anyone has any real code examples that would be awesome.

like image 894
Chev Avatar asked Jul 29 '11 18:07

Chev


1 Answers

Does the MockingKernel extension handle what you need? It has Moq, RhinoMocks, and NSubstitute flavors, and it is also available on NuGet.

like image 183
Ian Davis Avatar answered Oct 25 '22 02:10

Ian Davis