如何使用 IMappingAction 使用 AfterMap 对 AutoMapper 配置文件进行单元测试
How to unit test AutoMapper profile with AfterMap using IMappingAction
我如何对使用 AfterMap
和 IMappingAction
且具有注入服务的配置文件进行单元测试。
MappingProfile.cs
public MappingProfile()
{
CreateMap<string, string>()
.ConvertUsing<Core.Converter.TrimStringValueConverter>();
CreateMap<TestModel, TestEntity>(
.AfterMap<AfterMapAction>();
}
AfterMapAction.cs
public class AfterMapAction: IMappingAction<TestModel, TestEntity>
{
private readonly IAfterMapService _afterMapService ;
public AfterMapAction(IAfterMapService aftermapService)
{
_afterMapService = afterMapService ?? throw new ArgumentNullException(nameof(afterMapService));
}
public void Process(TestModel, TestEntity destination, ResolutionContext context)
{
var somevalue = _afterMapService.DoAction(source);
...
}
}
Test.cs
[TestMethod]
public void AutoMapperTest()
{
// Arrange
var model = new TestModel { Id = "4711" , Name = "Test" };
var mapper = new Mapper(new MapperConfiguration(cfg =>
{
cfg.AddProfile<MappingProfile>();
}));
// Act
var mappedObject = mapper.Map<Entity>(model);
...
}
当我 运行 这个测试时,我得到以下异常:
System.MissingMethodException: '没有为 'AfterMapAction' 类型定义无参数构造函数。
cfg.ConstructServicesUsing
的用法无效。它总是以 InvalidCastException 结尾。
如何安排单元测试,让AutoMapper可以实例化AfterMapAction
?
我可以通过在单元测试中使用 IServiceCollection
来解决这个问题。
// Arrange
var services = new ServiceCollection();
services.AddSingleton<AfterMapAction>();
services.AddSingleton<IAfterMapService, AfterMapService>();
services.AddAutoMapper(cfg => cfg.AddProfile<MappingProfile>());
var serviceProvider = services.BuildServiceProvider();
var mapper = serviceProvider.GetService<IMapper>();
// Act
我如何对使用 AfterMap
和 IMappingAction
且具有注入服务的配置文件进行单元测试。
MappingProfile.cs
public MappingProfile()
{
CreateMap<string, string>()
.ConvertUsing<Core.Converter.TrimStringValueConverter>();
CreateMap<TestModel, TestEntity>(
.AfterMap<AfterMapAction>();
}
AfterMapAction.cs
public class AfterMapAction: IMappingAction<TestModel, TestEntity>
{
private readonly IAfterMapService _afterMapService ;
public AfterMapAction(IAfterMapService aftermapService)
{
_afterMapService = afterMapService ?? throw new ArgumentNullException(nameof(afterMapService));
}
public void Process(TestModel, TestEntity destination, ResolutionContext context)
{
var somevalue = _afterMapService.DoAction(source);
...
}
}
Test.cs
[TestMethod]
public void AutoMapperTest()
{
// Arrange
var model = new TestModel { Id = "4711" , Name = "Test" };
var mapper = new Mapper(new MapperConfiguration(cfg =>
{
cfg.AddProfile<MappingProfile>();
}));
// Act
var mappedObject = mapper.Map<Entity>(model);
...
}
当我 运行 这个测试时,我得到以下异常:
System.MissingMethodException: '没有为 'AfterMapAction' 类型定义无参数构造函数。
cfg.ConstructServicesUsing
的用法无效。它总是以 InvalidCastException 结尾。
如何安排单元测试,让AutoMapper可以实例化AfterMapAction
?
我可以通过在单元测试中使用 IServiceCollection
来解决这个问题。
// Arrange
var services = new ServiceCollection();
services.AddSingleton<AfterMapAction>();
services.AddSingleton<IAfterMapService, AfterMapService>();
services.AddAutoMapper(cfg => cfg.AddProfile<MappingProfile>());
var serviceProvider = services.BuildServiceProvider();
var mapper = serviceProvider.GetService<IMapper>();
// Act