我有一个单元测试 - 我在设置中进行AutoMapperConfiguration。然后我将IMappingEngine设置为我的类中的构造函数中的私有属性,我实际上在其中进行映射。如果我使用此属性,单元测试失败,但使用来自automapper的静态方法可以正常工作。这两种方法在运行实际程序时都能正常工作。我能看到的唯一区别是单元测试是在一个单独的组件中。已启用CLS合规性。
public class AutomapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<AclassMappingProfile>();
});
}
public static void Reset()
{
Mapper.Reset();
}
}
public class AssetModelFactoryTests
{
[SetUp]
public void SetUp()
{
AutomapperConfiguration.Configure();
}
[Test]
public void TestA()
{
var a = new A();
}
}
public class A
{
private IMappingEngine _mappingEngine;
public A()
{
_mappingEngine = Mapper.Engine;
}
public void DoA()
{
Mapper.Map<Destination>(source); //works
_mappingEngine.Map<Destionation>(source); //Throws mapping not supported
}
}
答案 0 :(得分:1)
如果您计划测试方法,请不要使用静态.map(function (){}, this);
方法。在任何地方使用Mapper.*
,在应用程序启动时配置它,并通过适当的依赖转换机制注入。