Unit testing 如何使用xUnit为服务层编写测试?

Unit testing 如何使用xUnit为服务层编写测试?,unit-testing,asp.net-core,xunit,in-memory-database,service-layer,Unit Testing,Asp.net Core,Xunit,In Memory Database,Service Layer,如何使用xUnit为服务层编写测试?我正在为我的服务层使用依赖注入,但是如何使用内存中的数据库创建它的实例呢 到目前为止,我得到的是:我遵循这一点,下面是我使用服务层的想法,但它很混乱,我不知道如何简化它。我在所有控制器中使用服务的接口 [事实] public void Add_将_写入_数据库() { var options=new DbContextOptionsBuilder() .UseInMemoryDatabase(数据库名称:“添加\写入\到\数据库”) .选择; //对上下文的一

如何使用xUnit为服务层编写测试?我正在为我的服务层使用依赖注入,但是如何使用内存中的数据库创建它的实例呢

到目前为止,我得到的是:我遵循这一点,下面是我使用服务层的想法,但它很混乱,我不知道如何简化它。我在所有控制器中使用服务的接口

[事实]
public void Add_将_写入_数据库()
{
var options=new DbContextOptionsBuilder()
.UseInMemoryDatabase(数据库名称:“添加\写入\到\数据库”)
.选择;
//对上下文的一个实例运行测试
使用(var context=newapplicationdbcontext(选项))
{
var productRepo=新存储库(上下文);
var categoryRepo=新存储库(上下文);
var categoryMappingRepo=新存储库(上下文);
var categoryService=新的categoryService(上下文、categoryRepo、categoryMappingRepo);
var manufacturerRepo=新存储库(上下文);
var manufacturerMappingRepo=新存储库(上下文);
var manufacturerService=新的manufacturerService(上下文、manufacturerRepo、manufacturerMappingRepo);
var imageRepo=新存储库(上下文);
var imageMappingRepo=新存储库(上下文);
var imageService=新的ImageManagerService(imageRepo、imageMappingRepo);
var specificationRepo=新存储库(上下文);
var specificationMappingRepo=新存储库(上下文);
var specificationService=新的specificationService(上下文、specificationRepo、specificationMappingRepo);
var productService=新产品服务(上下文、产品报告、类别服务、制造商服务、图像服务、规范服务);
var product=newproduct(){Id=Guid.NewGuid(),Name=“Product1”,Price=100m};
productService.InsertProduct(产品);
}
//使用上下文的单独实例验证是否已将正确的数据保存到数据库中
使用(var context=newapplicationdbcontext(选项))
{
var productRepo=新存储库(上下文);
var categoryRepo=新存储库(上下文);
var categoryMappingRepo=新存储库(上下文);
var categoryService=新的categoryService(上下文、categoryRepo、categoryMappingRepo);
var manufacturerRepo=新存储库(上下文);
var manufacturerMappingRepo=新存储库(上下文);
var manufacturerService=新的manufacturerService(上下文、manufacturerRepo、manufacturerMappingRepo);
var imageRepo=新存储库(上下文);
var imageMappingRepo=新存储库(上下文);
var imageService=新的ImageManagerService(imageRepo、imageMappingRepo);
var specificationRepo=新存储库(上下文);
var specificationMappingRepo=新存储库(上下文);
var specificationService=新的specificationService(上下文、specificationRepo、specificationMappingRepo);
var productService=新产品服务(上下文、产品报告、类别服务、制造商服务、图像服务、规范服务);
Assert.Equal(1,productService.GetAllProduct().Count());
}
}

My
productService
与其他服务、存储库和上下文有很多依赖关系。

是的,您有很多依赖的服务和存储库。既然您已经在服务中使用依赖注入,我建议您使用IoC容器。这不仅可以删除大量用于设置集成测试的代码,还可以帮助您轻松解析应用程序中的任何服务

您可以为类型映射创建一个类,如下所示:

公共类服务
{
私有只读DbContextOptions\u选项;
专用只读IUnityContainer\u容器;
公共服务(DbContextOptions)
{
_选项=选项;
_容器=新的UnityContainer();
RegisterTypes();
}
私有无效注册表类型()
{
_RegisterType(新的ContainerControlledLifetimeManager(),新的InjectionConstructor(_选项));
_RegisterType(新的ContainerControlledLifetimeManager());
_RegisterType(新的ContainerControlledLifetimeManager());
_RegisterType(新的ContainerControlledLifetimeManager());
_RegisterType(新的ContainerControlledLifetimeManager());
//等等。。。
}
公共部门得不到
{
返回_container.Resolve();
}
}
这样,您就可以最小化测试中解析产品服务所需的代码:

[事实]
public void Add_将_写入_数据库()
{
var options=new DbContextOptionsBuilder()
.UseInMemoryDatabase(数据库名称:“添加\写入\到\数据库”)
.选择;
var服务=新服务(选项);
var target=services.Get();
//为你的测试干杯
}

我还没有在VS中测试和验证这些代码行,但它应该会让您有所了解。我们在其中一个应用程序中也使用Unity这种方法,您可以使用自己喜欢的IoC容器。您还需要存储库和服务的接口,但最好还是有它们:-)

我怎么做是使用默认的asp.net核心依赖项注入?