我想做单元测试检查automapper是否正常工作。创建地图
CreateMap<List<BaseProd.Product>, List<TL.StockQuantity>>()
.ConvertUsing<ProductStockQuantityConverter>();
转换器代码:
public class ProductStockQuantityConverter : ITypeConverter<List<BaseProd.Product>, List<TL.StockQuantity>>
{
private readonly IMapper mapper;
private readonly ProductService productService;
public ProductStockQuantityConverter(IMapper mapper, ProductService productService)
{
this.mapper = mapper;
this.productService = productService;
}
public List<TL.StockQuantity> Convert(List<BaseProd.Product> source, List<TL.StockQuantity> destination, ResolutionContext context)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
destination = new List<TL.StockQuantity>();
foreach (var item in source)
{
destination.Add(new TL.StockQuantity()
{
ProductOriginalId = item.ErplId,
Quantity = productService.GetQuantity(item.Id, ignorePresale: true).Quantity
});
}
return destination;
}
}
[Fact]
public void TestB2BStockQuantityEqual()
{
List<BaseProd.Product> prodList = new List<BaseProd.Product>();
List<TL.StockQuantity> stockQuantityList = new List<TL.StockQuantity>();
BaseProd.Product firstProductItem = new BaseProd.Product()
{
ErplId = ...
Quantity = ...
};
BaseProd.Product secondProductItem = new BaseProd.Product()
{
ErplId = ...
Quantity = ...
};
TL.StockQuantity firstStockQuantityItem = new TL.StockQuantity()
{
ProductOriginalId = ...
Quantity = ...
};
TL.StockQuantity secondStockQuantityItem = new TL.StockQuantity()
{
ProductOriginalId = ...
Quantity = ...
};
prodList.Add(firstProductItem);
prodList.Add(secondProductItem);
stockQuantityList.Add(firstStockQuantityItem);
stockQuantityList.Add(secondStockQuantityItem);
List<TL.StockQuantity> expected = mapper.Map<List<TL.StockQuantity>>(prodList);
Assert.Equal(expected, stockQuantityList);
}
public partial class StockQuantity : IEquatable<StockQuantity>
{
public bool Equals(StockQuantity other)
{
bool equals =
int.Equals(this.ProductOriginalId, other.ProductOriginalId) &&
decimal.Equals(this.Quantity, other.Quantity);
return equals;
}
}
现在的问题是错误“无参数构造函数”
我不能在转换器中使用无参数构造函数,即使我这样做了(在另一个示例中尝试了从db获取repo),我也会得到一个错误,即repo为null。我不知道怎样才能正确地做
编辑
public partial class StockQuantity
{
public int ProductOriginalId { get; set; }
public decimal Quantity { get; set; }
}
我在每个转换器中都遇到了这个问题,如果我正在创建映射并使用
.ForMember(...)
这没问题,但使用转换器会失败
编辑
public abstract class BaseAutomapperTest
{
public virtual bool IsConfigurationValid()
{
try
{
Mapper.AssertConfigurationIsValid();
return true;
}
catch
{
return false;
}
}
}
public abstract class BaseAutomapperTest<TProfile> : BaseAutomapperTest where TProfile : Profile, new()
{
protected MapperConfiguration config;
protected IMapper mapper;
public override bool IsConfigurationValid()
{
try
{
config.AssertConfigurationIsValid();
return true;
}
catch
{
return false;
}
}
public BaseAutomapperTest()
{
config = new MapperConfiguration(c => c.AddProfile<TProfile>());
mapper = new Mapper(config);
//config.AssertConfigurationIsValid<TProfile>();
}
}
从上到下用这个测试来上课
public class AutoMapperTests : BaseAutomapperTest<AutoMapperProfile>
{
public AutoMapperTests()
: base()
{
}
...
[Fact]
public void TestB2BStockQuantityEqual()
{
List<BaseProd.Product> prodList = new List<BaseProd.Product>();
List<TL.StockQuantity> stockQuantityList = new List<TL.StockQuantity>();
BaseProd.Product firstProductItem = new BaseProd.Product()
{
ErplId = 1,
Quantity = new[] { new ProductWarehouseQuantity() }
};
BaseProd.Product secondProductItem = new BaseProd.Product()
{
ErplId = 2,
Quantity = new[] { new ProductWarehouseQuantity() }
};
TL.StockQuantity firstStockQuantityItem = new TL.StockQuantity()
{
ProductOriginalId = 1,
Quantity = 1
};
TL.StockQuantity secondStockQuantityItem = new TL.StockQuantity()
{
ProductOriginalId = 2,
Quantity = 1
};
prodList.Add(firstProductItem);
prodList.Add(secondProductItem);
stockQuantityList.Add(firstStockQuantityItem);
stockQuantityList.Add(secondStockQuantityItem);
List<TL.StockQuantity> expected = mapper.Map<List<TL.StockQuantity>>(prodList);
Assert.Equal(expected, stockQuantityList);
}
...
}