如何在测试类中使用unity来使用构造函数依赖注入

发布于 2025-01-10 18:15:18 字数 1553 浏览 0 评论 0原文

我正在使用 Unity 容器在控制台应用程序中实现构造函数 DI。依赖关系注册是在程序类的 Main 方法中完成的。

一切正常,但我无法在测试类“ClientSearchTest”中实现构造函数 DI。我必须使用 new 运算符在测试类中创建一个新对象才能使其工作。

Unity 容器应该负责创建新对象,而不是使用 NEW 运算符手动创建它。

程序

static void Main(string[] args)
{
    // Register the dependencies
    container = RegisterDependency(new UnityContainer());

    var program  = container.Resolve<ExportClientDetails>();
    program.Run();
}

//register the type-mapping with the unity container
private static IUnityContainer RegisterDependency(IUnityContainer container)
{
    container.RegisterType<IClientOperation, ClientOperation>();
    container.RegisterType<IClientSearch, ClientSearch>();

    return container;
}

class ExportClientDetails
{
     private readonly IClientSearch _clientSearch;

     //Constructor Injection
     public ExportClientDetails(IClientSearch clientSearch)
     {
            this._clientSearch = clientSearch;
     }

}

public class ClientSearch : IClientSearch
{
   private readonly IClientOperation _clientOperation;
 
    //constructor
    public ClientSearch(IClientOperation clientOperation)
    {
        _clientOperation = clientOperation;
    }
}

测试

[TestClass]
public class ClientSearchTest
{
    private IClientSearch _clientSearch;
    private IClientOperation _clientOperation;

    [TestInitialize]
    public void TestInitialize()
    {
        _clientOperation = new ClientOperation();
        _clientSearch = new ClientSearch(_clientOperation);
    }

}

I am using Unity container to achieve the constructor DI in the console application. The dependency registration is done in the Main method of a program class.

All works fine but i am not able to achieve the constructor DI in the Test Class 'ClientSearchTest'. I have to create a new object in the test class using new operator to make it work.

Unity container should take care of creating a new object instead it being created manually using NEW operator.

Program

static void Main(string[] args)
{
    // Register the dependencies
    container = RegisterDependency(new UnityContainer());

    var program  = container.Resolve<ExportClientDetails>();
    program.Run();
}

//register the type-mapping with the unity container
private static IUnityContainer RegisterDependency(IUnityContainer container)
{
    container.RegisterType<IClientOperation, ClientOperation>();
    container.RegisterType<IClientSearch, ClientSearch>();

    return container;
}

class ExportClientDetails
{
     private readonly IClientSearch _clientSearch;

     //Constructor Injection
     public ExportClientDetails(IClientSearch clientSearch)
     {
            this._clientSearch = clientSearch;
     }

}

public class ClientSearch : IClientSearch
{
   private readonly IClientOperation _clientOperation;
 
    //constructor
    public ClientSearch(IClientOperation clientOperation)
    {
        _clientOperation = clientOperation;
    }
}

Test

[TestClass]
public class ClientSearchTest
{
    private IClientSearch _clientSearch;
    private IClientOperation _clientOperation;

    [TestInitialize]
    public void TestInitialize()
    {
        _clientOperation = new ClientOperation();
        _clientSearch = new ClientSearch(_clientOperation);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文