C# 尚未在xUnit测试中初始化Automapper实例配置
我正在用xUnit在WebAPI Asp.Net内核上编写一些单元测试,并测试我的服务 我创建了一个生成器类,它创建映射器实例,以便在构造函数中创建需要IMapper的类C# 尚未在xUnit测试中初始化Automapper实例配置,c#,asp.net,automapper,xunit,C#,Asp.net,Automapper,Xunit,我正在用xUnit在WebAPI Asp.Net内核上编写一些单元测试,并测试我的服务 我创建了一个生成器类,它创建映射器实例,以便在构造函数中创建需要IMapper的类 public IMapper Mapper() { var config = new MapperConfiguration(cfg => { cfg.AddProfile<CustomerRoleProfile>();
public IMapper Mapper()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<CustomerRoleProfile>();
cfg.AddProfile<LicenseProfile>();
cfg.AddProfile<TaxExemptionProfile>();
cfg.AddProfile<BankProfile>();
cfg.AddProfile<AddressProfile>();
cfg.AddProfile<CustomerDetailsProfile>();
cfg.AddProfile<CustomerProfile>();
});
return config.CreateMapper();
}
如果我尝试断言配置,所有测试都会失败
config.AssertConfigurationIsValid();
但是如果我尝试使用具有相同配置的静态实例,它不会失败,但是一些测试会失败,因为如果我有更多的测试类,Automapper已经初始化
public IMapper Mapper()
{
AutoMapper.Mapper.Reset();
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.AddProfile(new CustomerRoleProfile());
cfg.AddProfile(new LicenseProfile());
cfg.AddProfile(new TaxExemptionProfile());
cfg.AddProfile(new BankProfile());
cfg.AddProfile(new AddressProfile());
cfg.AddProfile(new CustomerDetailsProfile());
cfg.AddProfile(new CustomerProfile());
});
return Automapper.Mapper.Configuration.CreateMapper();
}
例如,使用静态Automapper,来自一个类的所有测试都是成功的
[Fact]
public async Task UpdateOrInsertCustomer()
{
var customer = new CustomerCreateDto() { CustomerId = 1, StoreId = 1, CardTypeCode = "GO", InvoiceTypeCode = "PRO", SelfScanningAllowed = true, TradeId = 12345, CountryCode = "ROU" };
var result = await _customerService.UpdateOrInsert(customer);
result.Should().BeTrue();
}
另一个类的一些测试失败了,比如这个
[Theory]
[InlineData(1, 1, null)]
public async Task GeValidCustomerDetails(int customerId, int storeId, int? cardHolderId)
{
var result = await _detailsService.GetAsync(customerId, storeId, cardHolderId);
if (!cardHolderId.HasValue)
result.Should().NotBeNull().And.Subject.Should().BeOfType<OrganizationDto>();
else
result.Should().NotBeNull().And.Subject.Should().BeOfType<PersonDto>()
.And.Subject.As<PersonDto>().CardHolderId.Should().Be(cardHolderId.Value);
result.CustomerId.Should().Be(customerId);
result.StoreId.Should().Be(storeId);
}
[理论]
[InlineData(1,1,null)]
公共异步任务GeValidCustomerDetails(int customerId、int storeId、int?持卡人ID)
{
var result=wait_detailsService.GetAsync(customerId、storeId、持卡人ID);
如果(!持卡人ID.HasValue)
result.Should().NotBeNull()和.Subject.Should().BeOfType();
其他的
result.Should().NotBeNull()和.Subject.Should().BeOfType()的
.和.Subject.As().cardingID.Should().Be(cardingID.Value);
result.CustomerId.Should().Be(CustomerId);
result.StoreId.Should().Be(StoreId);
}
作为一种解决方法,我使用了带有静态包装器和锁的静态实例来避免争用情况,它适用于所有单元测试。
我不知道为什么映射器配置实例不工作 作为一种解决方法,我使用了带有静态包装器和锁的静态实例来避免竞争条件,它适用于所有单元测试。
我不知道为什么映射器配置实例不工作 您应该切换到基于实例的API。当我尝试使用mapper配置实例时,断言配置有效失败。仅在单元测试中。我只在xUnit项目中使用这个静态实例,你不应该使用任何静态的东西。检查。您应该切换到基于实例的API。当我尝试使用mapper配置实例时,断言配置有效失败。仅在单元测试中。我只在xUnit项目中使用这个静态实例,你不应该使用任何静态的东西。检查
[Theory]
[InlineData(1, 1, null)]
public async Task GeValidCustomerDetails(int customerId, int storeId, int? cardHolderId)
{
var result = await _detailsService.GetAsync(customerId, storeId, cardHolderId);
if (!cardHolderId.HasValue)
result.Should().NotBeNull().And.Subject.Should().BeOfType<OrganizationDto>();
else
result.Should().NotBeNull().And.Subject.Should().BeOfType<PersonDto>()
.And.Subject.As<PersonDto>().CardHolderId.Should().Be(cardHolderId.Value);
result.CustomerId.Should().Be(customerId);
result.StoreId.Should().Be(storeId);
}