无法模拟.Net核心中的自动映射器



net core 3.1和我正在使用自动映射器。我正在编写单元测试用例,下面有一段代码

var coOrdinateReferenceSystemNames = await _coordinateReferenceSystemRepository.GetAsync(x => x.CoordinateReferenceSystemName.Contains(request.searchName)).ConfigureAwait(false);
IEnumerable<CoordinateReferenceSystemModel> CoordinateReferenceSystemList = _mapper.Map<IEnumerable<CoordinateReferenceSystemModel>>(coOrdinateReferenceSystemNames);

下面是我的单元测试用例。

Func<IEnumerable<CoordinateReferenceSystem>> func = () => { return new List<CoordinateReferenceSystem>() { coordinateReferenceSystemModel }; };
this.mockMapper.Setup(x => x.Map<IEnumerable<CoordinateReferenceSystem>>(It.IsAny<IEnumerable<CoordinateReferenceSystemModel>>())).Returns(func);

哪个抛出低于错误

One or more errors occurred. (IMapperBase.Map>([CoordinateReferenceSystem]) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.)

有人能帮我理解这个错误吗?我在这里做错了什么。如有任何帮助,我们将不胜感激。感谢

嗨,我添加了下面的mock mapper,并开始正常工作。

private CoordinateReferenceSystemAr CreateCoordinateReferenceSystemAr()
{
var mockMapper = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfile());
});
var mapper = mockMapper.CreateMapper();
return new CoordinateReferenceSystemAr(this.mockApiRequestHandler.Object,this.mockUnitOfWork.Object, mapper);
}

最新更新