小编wom*_*72a的帖子

将 AutoMapper 从 v6 升级到 v9 并使用解析上下文进行单元测试

我希望有人能帮助我。我们目前正在将 AutoMapper 从 v6 升级到 v9 - 我们会升级到 v10,但无法创建新的ResolutionContext影响我们的单元测试。也就是说,在 v9 中,我们在单元测试 AutoMapper 转换器方面仍然遇到以下问题......

转换器类:

public class FooBarConverter :
    ITypeConverter<FooSourceObject, BarDestinationObject>
{
    /// <inheritdoc/>
    public virtual BarDestinationObjectConvert(FooSourceObjectsource, BarDestinationObjectdestination, ResolutionContext context)
    {
        EnsureArg.IsNotNull(source, nameof(source));

        switch (source.Type)
        {
            case SomeType.None:
                return context.Mapper.Map<NoneBarDestinationObject>(source);
            case SomeType.FixedAmount:
                return context.Mapper.Map<FixedBarDestinationObject>(source);
            case SomeType.Percentage:
                return context.Mapper.Map<PercentageBarDestinationObject>(source);
            default:
                throw new ArgumentOutOfRangeException(nameof(source));
        }
    }
Run Code Online (Sandbox Code Playgroud)

在 AutoMapper 6 之前,我们进行了以下单元测试(使用 Xunit):

public class FooBarConverterTests
{
    private readonly FooBarConverter target;

    private readonly Mock<IRuntimeMapper> mockMapper;
    private readonly ResolutionContext resolutionContext;

    public FooBarConverterTests()
    {
        this.mockMapper …
Run Code Online (Sandbox Code Playgroud)

automapper-9

4
推荐指数
1
解决办法
2125
查看次数

标签 统计

automapper-9 ×1