2

我目前正在尝试使用 Unity 和 NSubstitute 学习 DI 和 Mocking。我还使用了一个取自这个问题的自动模拟扩展:Unity(而不是 Castle Windsor)可以做到这一点吗?

在下面的单元测试中,我试图从我的方法 Add() 中设置 NSubstitute 返回值 10。但是,当通过控制器调用进行调试时,分配的值是默认的 0 而不是预期的 10。代理似乎没有拦截方法调用。

我怀疑这是由于没有正确注册我的类型/容器造成的,有人能指出我正确的方向吗?

[TestFixture]
public class ApplicationControllerTests
{
    private IUnityContainer _container;
    private ApplicationController _controller;
    private ISampleService _sampleService;

    [SetUp]
    public void SetUp()
    {
        _container = new UnityContainer().AddExtension(new AutoMockingContainerExtension());
        _controller = _container.Resolve<ApplicationController>();
        _sampleService = _container.Resolve<ISampleService>();
    }

    [Test]
    public void TestSampleService()
    {
        // This line is not working
        _sampleService.Add(Arg.Any<int>(), Arg.Any<int>()).Returns(10);

        var result = _controller.Index();

        _sampleService.Received().Add(Arg.Any<int>(), Arg.Any<int>());
    }
}

public class AutoMockingContainerExtension : UnityContainerExtension
{
    protected override void Initialize()
    {
        var strategy = new AutoMockingBuilderStrategy(Container);

        Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
    }

    class AutoMockingBuilderStrategy : BuilderStrategy
    {
        private readonly IUnityContainer _container;

        public AutoMockingBuilderStrategy(IUnityContainer container)
        {
            _container = container;
        }

        public override void PreBuildUp(IBuilderContext context)
        {
            var key = context.OriginalBuildKey;

            if (key.Type.IsInterface && !_container.IsRegistered(key.Type))
                context.Existing = CreateSubstitute(key.Type);
        }

        private static object CreateSubstitute(Type type)
        {
            return Substitute.For(new[] { type }, null);
        }
    }
}

还有我的控制器代码

public class ApplicationController : BaseController
{
    private readonly ISampleService _sampleService;

    public ApplicationController(ISampleService sampleService)
    {
        _sampleService = sampleService;
    }

    public ActionResult Index()
    {
        var result = _sampleService.Add(2, 3);

        // result is 0, expected 10 ??

        return View();
    }
}

public interface ISampleService
{
    int Add(int first, int second);
}

public class SampleService : ISampleService
{
    public int Add(int first, int second)
    {
        return first + second;
    }
}
4

2 回答 2

4

实际上 Tormod 是对的,问题是AutoMockingBuilderStrategy 每次有人请求ISampleService容器表单时都会返回一个不同的模拟实例。

所以在我的原始实现中存在一个错误,即AutoMockingBuilderStrategy不存储创建的模拟:

这是一个固定版本:

public class AutoMockingContainerExtension : UnityContainerExtension
{
    protected override void Initialize()
    {
        var strategy = new AutoMockingBuilderStrategy(Container);

        Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
    }

    class AutoMockingBuilderStrategy : BuilderStrategy
    {
        private readonly IUnityContainer container;
        private readonly Dictionary<Type, object> substitutes 
           = new Dictionary<Type, object>();

        public AutoMockingBuilderStrategy(IUnityContainer container)
        {
            this.container = container;
        }

        public override void PreBuildUp(IBuilderContext context)
        {
            var key = context.OriginalBuildKey;

            if (key.Type.IsInterface && !container.IsRegistered(key.Type))
            {
                context.Existing = GetOrCreateSubstitute(key.Type);
                context.BuildComplete = true;
            }
        }

        private object GetOrCreateSubstitute(Type type)
        {
            if (substitutes.ContainsKey(type))
                return substitutes[type];

            var substitute = Substitute.For(new[] {type}, null);

            substitutes.Add(type, substitute);

            return substitute;
        }
    }
}
于 2012-11-14T06:23:27.913 回答
1

我怀疑您正在处理两个不同的 ISampleService 实例,它们分别在 Setup 的第 2 行和第 3 行中创建。为了测试,您能否公开 _sampleServie 字段并在第三个 Setup() 行中尝试

_sampleService = _controller._sampleService;
于 2012-11-14T06:11:47.417 回答