1
如何使用xUnit编写测试用于服务层?我为我的服务层使用依赖注入,但是如何在内存数据库中使用它的实例呢?这是我到目前为止,我遵循这个link,这里是我来使用我的服务层,但它是一团糟,我不知道如何简化它。使用即时通讯服务的接口,在我所有的控制器如何使用xUnit编写测试用于服务层?
[Fact]
public void Add_writes_to_database()
{
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(databaseName: "Add_writes_to_database")
.Options;
// Run the test against one instance of the context
using (var context = new ApplicationDbContext(options))
{
var productRepo = new Repository<Product>(context);
var categoryRepo = new Repository<Category>(context);
var categoryMappingRepo = new Repository<ProductCategoryMapping>(context);
var categoryService = new CategoryService(context, categoryRepo, categoryMappingRepo);
var manufacturerRepo = new Repository<Manufacturer>(context);
var manufacturerMappingRepo = new Repository<ProductManufacturerMapping>(context);
var manufacturerService = new ManufacturerService(context, manufacturerRepo, manufacturerMappingRepo);
var imageRepo = new Repository<Image>(context);
var imageMappingRepo = new Repository<ProductImageMapping>(context);
var imageService = new ImageManagerService(imageRepo, imageMappingRepo);
var specificationRepo = new Repository<Specification>(context);
var specificationMappingRepo = new Repository<ProductSpecificationMapping>(context);
var specificationService = new SpecificationService(context, specificationRepo, specificationMappingRepo);
var productService = new ProductService(context, productRepo, categoryService, manufacturerService, imageService, specificationService);
var product = new Product() { Id = Guid.NewGuid(), Name = "Product1", Price = 100m };
productService.InsertProduct(product);
}
// Use a separate instance of the context to verify correct data was saved to database
using (var context = new ApplicationDbContext(options))
{
var productRepo = new Repository<Product>(context);
var categoryRepo = new Repository<Category>(context);
var categoryMappingRepo = new Repository<ProductCategoryMapping>(context);
var categoryService = new CategoryService(context, categoryRepo, categoryMappingRepo);
var manufacturerRepo = new Repository<Manufacturer>(context);
var manufacturerMappingRepo = new Repository<ProductManufacturerMapping>(context);
var manufacturerService = new ManufacturerService(context, manufacturerRepo, manufacturerMappingRepo);
var imageRepo = new Repository<Image>(context);
var imageMappingRepo = new Repository<ProductImageMapping>(context);
var imageService = new ImageManagerService(imageRepo, imageMappingRepo);
var specificationRepo = new Repository<Specification>(context);
var specificationMappingRepo = new Repository<ProductSpecificationMapping>(context);
var specificationService = new SpecificationService(context, specificationRepo, specificationMappingRepo);
var productService = new ProductService(context, productRepo, categoryService, manufacturerService, imageService, specificationService);
Assert.Equal(1, productService.GetAllProduct().Count());
}
}
我productService
有很多依赖于其他服务,资源库和背景。
任何想法,我该怎么办,这是我使用默认的asp.net核心依赖注入? – kram005