如何在单元测试中从 Automapper 模拟 IValueResolver 的依赖关系

问题描述 投票:0回答:1

上一个问题之后,我有一个简单的实现

IValueResolver

public class FileLinkResolver : IValueResolver<Configuration, ConfigurationDto, string>
{
    private readonly IFileStorage _fileStorage;

    public FileLinkResolver(IFileStorage fileStorage)
    {
        _fileStorage = fileStorage;
    }

    public string Resolve(Configuration source, ConfigurationDto destination, string destMember, ResolutionContext context)
    {
        return _fileStorage.GetShortTemporaryLink(source.Path);
    }
}

和简单的映射配置文件

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<Configuration, ConfigurationDto>()
            .ForMember(dest => dest.FilePath, opt => opt.MapFrom<FileLinkResolver>());
    }
}

对于生产,当进行以下设置时,它可以按预期工作

services.AddTransient<IFileStorage>(...);
services.AddAutoMapper(); 
使用

,然后在控制器

IMapper
中注入。

在单元测试中我尝试存根映射器

var mapperStub = new Mapper(new MapperConfiguration(map => map.AddProfile(new MappingProfile())));

当我对应该返回映射的 dto 的方法运行测试时,我得到了

AutoMapper.AutoMapperMappingException:映射类型错误。

Mapping types:
Configuration -> ConfigurationDto
DataAccess.Models.Configuration -> Dto.ConfigurationDto

Type Map configuration:
Configuration -> ConfigurationDto
DataAccess.Models.Configuration -> Dto.ConfigurationDto

Destination Member:
FilePath

---- System.MissingMethodException : No parameterless constructor defined for this object.

我尝试向

FileLinkResolver
添加无参数构造函数,但是,
NullReferenceException

这就是问题:如何解决 ValueResolver 的依赖关系

c# .net asp.net-core dependency-injection automapper
1个回答
12
投票

在当前示例中,映射器在测试时无法解析

IFileStorage
依赖关系。

更改映射器的创建方式,使其更接近于运行时的完成方式。

IServiceCollection services = new ServiceCollection();
//mocking service using MOQ
var mock = Mock.Of<IFileStorage>(_ => 
    _.GetShortTemporaryLink(It.IsAny<string>()) == "fake link"
);
//adding mock to service collection.
services.AddTransient<IFileStorage>(sp => mock);

//adding auto mapper with desired profile;
services.AddAutoMapper(typeof(MappingProfile));

//...add other dependencies as needed to service collection.

//...

//build provider
IServiceProvider serviceProvider = services.BuildServiceProvider();

//resolve mapper
IMapper mapperStub = serviceProvider.GetService<IMapper>();

//Or resolve subject under test where mapper is to be injected
SubjectClass subject = serviceProvider.GetService<SubjectClass>();
//assuming ctr: public SubjectClass(IMapper mapper, .....) { ... }

现在从技术上讲,这并不是在嘲笑值解析器。它模拟解析器的依赖关系,并使用配置文件中的实际解析器。但这应该在测试目标时提供所需的行为。

© www.soinside.com 2019 - 2024. All rights reserved.