如何在测试类中使用unity来使用构造函数依赖注入
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论