小编Der*_*i81的帖子

如何使用 IMappingAction 通过 AfterMap 对 AutoMapper 配置文件进行单元测试

我如何对AfterMapIMappingAction注入服务一起使用的配置文件进行单元测试。

映射配置文件.cs

public MappingProfile()
{
    CreateMap<string, string>()
        .ConvertUsing<Core.Converter.TrimStringValueConverter>();   
    CreateMap<TestModel, TestEntity>(
        .AfterMap<AfterMapAction>();
}
Run Code Online (Sandbox Code Playgroud)

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);
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

测试.cs

[TestMethod]
public void AutoMapperTest()
{
    // Arrange
    var model = new TestModel { Id = "4711" , Name = "Test" };
    var mapper = …
Run Code Online (Sandbox Code Playgroud)

.net c# automapper .net-core

7
推荐指数
1
解决办法
2592
查看次数

标签 统计

.net ×1

.net-core ×1

automapper ×1

c# ×1