English 中文(简体)
Auto-mock container: Rhino Mocks and NInject
原标题:

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

最佳回答

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
}
问题回答




相关问题
How to unit test file permissions in NUnit?

I m trying to unit test file read operations. In this scenario I also need make sure that, if a particular user don t have read access he should get an exception... But somehow I m unable to get it ...

Rhino mocks - does this test look sensible?

I have just crafted the following test using Rhino mocks. Does my test look valid and make sense to those more experienced with mocking? I am a a little confused that I haven t had to use the ...

How to mock Add method of subsonic s SimpleRepository

I m trying to mock the Add method of subsonic SimpleRepository with Rihino mocks, I m using the IRepository Interface but I m new to mocking and dont know how to go from there, can this be done? ...

StrucutureMap RhinoMock Record/Playback, Example needed

I m looking for some examples on how to do the following Mock Tests using StructureMap or Unity with NUnit. I have the following code structure public interface IDAL { List<Model> Method1(...

How do I Fake UpdateModel for ASP.Net MVC?

I am trying to unit test a controller action that uses UpdateModel but I am not correctly mocking the HttpContext. I keep getting the following exception: System.InvalidOperationException: ...

热门标签