Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-mock container: Rhino Mocks and NInject

Does anyone have an implementation lying around of an auto-mock container using Rhino Mocks and NInject?

like image 645
cbp Avatar asked Nov 30 '09 07:11

cbp


2 Answers

OK I built one myself using the Moq integration as a starting point. It is very simple. You need these 3 classes:

public class AutoMockingKernel : StandardKernel
{
    private readonly IMockProvider _mockProvider;

    public void Reset()
    {
        Components.Get<ICache>().Clear();
    }

    protected override bool HandleMissingBinding(Type service)
    {
        var isSelfBindable = TypeIsSelfBindable(service);

        var binding = new Binding(service)
        {
            ProviderCallback = isSelfBindable
                                ? StandardProvider.GetCreationCallback(service)
                                : _mockProvider.GetCreationCallback(),
            IsImplicit = true
        };

        if (!isSelfBindable)
            binding.ScopeCallback = ctx => null;

        AddBinding(binding);

        return true;
    }

    public AutoMockingKernel(IMockProvider mockProvider, INinjectSettings settings, params INinjectModule[] modules)
        : base(settings, modules)
    {
        _mockProvider = mockProvider;
    }

    public AutoMockingKernel(IMockProvider mockProvider, params INinjectModule[] modules)
        : base(modules)
    {
        _mockProvider = mockProvider;
    }
}

internal class RhinoMockProvider : IProvider
{
    public Type Type { get; private set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="RhinoMockProvider"/> class.
    /// </summary>
    public RhinoMockProvider(Type type)
    {
        Type = type;
    }

    public object Create(IContext context)
    {
        return MockRepository.GenerateMock(Type, Type.EmptyTypes);
    }
}

public class RhinoAutoMockProvider : IMockProvider
{
    public Func<IContext, IProvider> GetCreationCallback()
    {
        return ctx => new RhinoMockProvider(ctx.Request.Service);
    }
}

You can then create an auto-mocking kernel in your unit test like this:

[Test]
public void Test()
{
    var kernel = new AutoMockingKernel(new RhinoAutoMockProvider());
    ... etc
}
like image 177
cbp Avatar answered Sep 20 '22 21:09

cbp


There is a RhinoMocks integration extension available.

like image 27
Ian Davis Avatar answered Sep 21 '22 21:09

Ian Davis