NSubstitute:无法为 IMapperBase.Map 返回 Task`1 类型的值预期类型 List`1

问题描述

我写了一个单元测试,在测试中我想测试我的 CashCapitalService

在我的服务中,我使用 AutoMapper 将我的模型映射到 dto 和反向。

我的服务是:

public class CashCapitalService : ICashCapitalService
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly IMapper _mapper;
    public CashCapitalService(IUnitOfWork unitOfWork,IMapper mapper)
    {
        _unitOfWork = unitOfWork;
        _mapper = mapper;
    }

    public async Task<List<CashCapitalInsertAndUpdateDto>> GetAllAsync()
    {
        var allAsync = await _unitOfWork.CashCapitalRepository.GetAllAsync();

        List<CashCapitalInsertAndUpdateDto> cashCapitalInsertAndUpdateDtos = _mapper.Map<List<CashCapitalInsertAndUpdateDto>>(source:allAsync);
        return cashCapitalInsertAndUpdateDtos;
    }
}
    
public class CashCapitalService : ICashCapitalService
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly IMapper _mapper;
    public CashCapitalService(IUnitOfWork unitOfWork,IMapper mapper)
    {
        _unitOfWork = unitOfWork;
        _mapper = mapper;
    }

    public async Task<List<CashCapitalInsertAndUpdateDto>> GetAllAsync()
    {
        var allAsync = await _unitOfWork.CashCapitalRepository.GetAllAsync();

        List<CashCapitalInsertAndUpdateDto> cashCapitalInsertAndUpdateDtos = _mapper.Map<List<CashCapitalInsertAndUpdateDto>>(source:allAsync);
        return cashCapitalInsertAndUpdateDtos;
    }
}

如您所见,Automapper 配置文件是:

public class CashCapitalInsertDtoProfile : Profile
{
    public CashCapitalInsertDtoProfile()
    {
        CreateMap<CashCapitalInsertAndUpdateDto,CashCapital>().ReverseMap();
    }
}

在我的单元测试中:

public class CashCapitalServiceTest
{
    private readonly CashCapitalService _cashCapitalService;
    private readonly IUnitOfWork _unitOfWork;
    private readonly IMapper _mapper;

    public CashCapitalServicetest()
    {
        _unitOfWork = Substitute.For<IUnitOfWork>();
        _mapper = Substitute.For<IMapper>();
        _cashCapitalService = new CashCapitalService(_unitOfWork,_mapper);
    }

    [Fact]
    public async Task GetAllAsync_Should_GetAllCashCapital()
    {
        //Arrange
        _unitOfWork.CashCapitalRepository.GetAllAsync().Returns(new List<CashCapital>());

        var cashCapitals = new List<CashCapital>()
        {
            new CashCapital()
            {
                InsertDateTime = DateTime.Now,AmountRial = 250000000,Description = "tv,a lj,v"
            }
        };

        var cashCapitalInsertAndUpdateDtos = new List<CashCapitalInsertAndUpdateDto>()
        {
            new CashCapitalInsertAndUpdateDto()
            {
                AmountRial = 250000000,v"
            }
        };

        //var returnValue = _mapper.Map(Arg.Any<List<CashCapital>>(),Arg.Any<List<CashCapitalInsertAndUpdateDto>>());
        //_cashCapitalService.GetAllAsync().Returns(returnValue);
        _mapper.Map(cashCapitals,cashCapitalInsertAndUpdateDtos).Returns(new List<CashCapitalInsertAndUpdateDto>()
        {
            new CashCapitalInsertAndUpdateDto()
            {
                AmountRial = 250000000,v"
            }
        });
        var resultValue = _mapper.Map(cashCapitals,cashCapitalInsertAndUpdateDtos);

        _cashCapitalService.GetAllAsync().ReturnsForAnyArgs(resultValue);
        //Act
        var all = await _cashCapitalService.GetAllAsync();

        //Assert
        all.Should().BeOfType<List<CashCapitalInsertAndUpdateDto>>();
        await _unitOfWork.CashCapitalRepository.Received(1).GetAllAsync();
    }
}

当我运行这个测试时,失败并给我一个错误

留言: NSubstitute.Exceptions.CouldNotSetReturnDuetoTypeMismatchException:无法返回 Task1 for IMapperBase.Map (expected type List1 类型的值。

我是单元测试和 NSubstitute 库的新手,我不知道如何模拟 AutoMapper。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)