我想做单元测试,检查自动映射器工作正常。创建的地图
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);
}
和Equals方法
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获取回购的示例中进行了尝试),我收到一个错误,回购为null。我不知道该怎么做
第二部分类代码:
public partial class StockQuantity
{
public int ProductOriginalId { get; set; }
public decimal Quantity { get; set; }
}
如果我要创建地图并使用
,我在每个Converter中都会遇到这个问题.ForMember(...)
可以,但是使用转换器失败
错误详细信息
我的BaseAutomapperTest类代码
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);
}
...
}
答案 0 :(得分:0)
我想告诉您的是,如果您使用的是Autofac,则可以直接在ProductStockQuantityConveter中解决依赖关系
例如
public class ProductStockQuantityConverter
{
private readonly ProductService productservice = Container.Resolve<ProductService>();
private readonly IMapper mapper = Container.Resolve<IMapper>();
public ProductStockQuantityConverter()
{
}
}