为涉及命名Unity资源的单元测试设置模拟的正确方法是什么?

时间:2014-02-21 23:37:49

标签: c# unity-container moq mstest automoq

我想编写一个单元测试来检查特定的Unity IoC对象是否已实例化。

例如,这是我正在测试的类。

using Microsoft.Practices.Unity;
using System.Threading;

namespace Client.Engine
{
    public sealed class Uploader : IUploader
    {
        private readonly IUnityContainer _container;

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

        public void PerformUpload(CancellationToken token, int batchSize)
        {
            token.ThrowIfCancellationRequested();
            _container.Resolve<IUploadModule>("Clients").Upload(token, batchSize);

            token.ThrowIfCancellationRequested();
            _container.Resolve<IUploadModule>("Patients").Upload(token, batchSize);
        }
    }
}

这是我设置的单元测试

[TestClass()]
public class UploadClientsTests : UploadModuleTestsBase
{
    [TestMethod()]
    public override void UploaderRegrestrationTest()
    {
        PerformRegistrationTest("Clients");
    }
}

[TestClass()]
public class UploadPatientsTests : UploadModuleTestsBase
{
    [TestMethod()]
    public override void UploaderRegrestrationTest()
    {
        PerformUpladerRegistrationTest("Patients");
    }
}

public class UploadPatientsTests : UploadModuleTestsBase
{
    protected static void PerformUpladerRegistrationTest(string moduleName)
    {
        var container = new UnityContainer();
        var mocker = new AutoMoqer(container);
        var random = new Random();
        int batchSize = random.Next(int.MaxValue);
        var token = new CancellationToken();

        var uploadModuleMock = new Mock<IUploadModule>();
        uploadModuleMock.Setup(module => module.Upload(token, batchSize)).Verifiable();

        container.RegisterInstance(typeof(IUploadModule), moduleName, uploadModuleMock.Object, new ContainerControlledLifetimeManager());
        container.RegisterInstance(typeof(IUploadModule), Mock.Of<IUploadModule>());
        var uploader = mocker.Resolve<Uploader>();
        uploader.PerformUpload(token, batchSize);

        uploadModuleMock.Verify();
    }
}

我遇到的问题是,如果命名类型不可用,Unity 2.0不会回退到默认实例。因此,如果我注释掉_container.Resolve<IUploadModule>("Patients")行客户端测试工作完美,如果我评论_container.Resolve<IUploadModule>("Clients")患者测试工作完美,我只是不知道怎么做,所以两者都可以共存


编辑:在正常操作中,我正在重新绑定这两个对象。

public static Bootstrap(IUnityContainer container)
{
    container.RegisterType<IUploadModule, UploadClients>("Clients", new HierarchicalLifetimeManager());
    container.RegisterType<IUploadModule, UploadPatients>("Patients", new HierarchicalLifetimeManager());
}

2 个答案:

答案 0 :(得分:3)

为什么不直接模拟IUnityContainer,而不是使用真正的UnityContainer?

从单元测试的角度来看,PerformUpload方法只能断言确实调用Resolve<>Upload方法并使用相应的参数。不是Unity解析的实际内部结构。

实际上你甚至可以删除对IUnityContainer的依赖,并让你的构造函数接收一个工厂方法,你可以在Unity中注册。 我倾向于避免这种依赖,并将工厂直接声明为Func<string, IUploadModule>

以下示例未经测试,但以下内容可能有效:

container.RegisterType<Func<string, IUploadModule>>(new InjectionFactory(c => 
    new Func<string, IUploadModule>(name => 
        c.Resolve<IUploadModule>(name)
        )
    )
);

这是一个额外的注册。您还必须像现在一样注册您的上传者。

注册后,您可以让构造函数使用Func<string, IUploadModule>作为参数:

public Uploader(Func<string, IUploadModule> factory)
{
    _factory = factory;
}

以下列方式调用它以获取您的上传器:

_factory("Patients");

答案 1 :(得分:0)

在没有看到应用程序中的所有内容设计的情况下,IMO我认为您的Uploader类应该重新构建一下。 PerformUpload方法与IUploadModule具有相关性,但不是像IoC那样将它们作为依赖项,而是在Uploader中手动解析它们。如果您有一个新的IUploadModule,则必须更改PerformUpload方法,这样做会破坏执行IoC的目的。

我的建议是这样的:

using Microsoft.Practices.Unity;
using System.Threading;

namespace Client.Engine
{
    public sealed class Uploader : IUploader
    {
        public Uploader()
        {
        }

        public void PerformUpload(IUploadModule uploadModule, CancellationToken token, int batchSize)
        {
            token.ThrowIfCancellationRequested();
            uploadModule.Upload(token, batchSize);
        }
    }
}

这也将简化您的单元测试。

[TestMethod()]
public void PerformUpladerRegistrationTest()
{
    var random = new Random();
    int batchSize = random.Next(int.MaxValue);
    var token = new CancellationToken();

    var uploadModuleMock = new Mock<IUploadModule>();
    uploadModuleMock.Setup(module => module.Upload(token, batchSize)).Verifiable();

    uploader.PerformUpload(uploadModuleMock.Object, token, batchSize);

    uploadModuleMock.Verify();
}