我正在尝试通过包含 AutoMapper 使用的存储库测试从我的 ASP.NET Core 控制器到数据库的路径。这是我的存储库:using System;using System.Linq;using AutoMapper;using DS.DTO.MasterData;using DS.Utilities.DSExceptions;using Microsoft.Extensions.Logging;using Omu.ValueInjecter;namespace DS.MasterData.Repositories{ public class PersonFactRepository : IPersonFactRepository { private readonly Database.MasterDataContext dbContext; private readonly ILogger<PersonFactRepository> logger; private readonly IMapper mapper; public PersonFactRepository(ILogger<PersonFactRepository> logger, Database.MasterDataContext dbcontext, IMapper mapper) { this.dbContext = dbcontext; this.mapper = mapper; this.logger = logger; } public PatientDto CreatePatient(CreatePatientDto inModel) { var dbPersonDim = mapper.Map<CreatePatientDto, Database.PersonDim>(inModel); var dbAddressDim = mapper.Map<CreatePatientDto, Database.AddressDim>(inModel); var dbPhoneDim = mapper.Map<CreatePatientDto, Database.PhoneDim>(inModel); var dbPersonFact = new Database.PersonFact { FactId = Guid.NewGuid() }; dbPersonDim.PersonFact = dbPersonFact; dbAddressDim.PersonFact = dbPersonFact; dbPhoneDim.PersonFact = dbPersonFact; } }}我的问题是如何将我的 Mapper 配置放入我的 fakeMapper 中?var inMemDB = Fakes.FakeDB.DB;var loggerCntl = Substitute.For<ILogger<Controllers.PatientController>>();var loggerPersonFactRepo = Substitute.For<ILogger<Repositories.PersonFactRepository>>();AutoMapper.IMapper fakeMapper = ????var personRepo = new PersonFactRepository(loggerPersonFactRepo, inMemDB, fakeMapper);我有意进行集成测试,因为我希望我的测试能够将数据发布到控制器并测试正确的数据是否最终出现在正确的表中。
1 回答
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
创建映射器提供程序
public class MapperProvider
{
public MapperProvider() {
}
public MapperConfiguration GetMapperConfig()
{
var mce = new MapperConfigurationExpression();
mce.AddProfile<AutoMapperConfig>();
var mc = new MapperConfiguration(mce);
return mc;
}
}
注册映射配置
AutoMapper.IMapper fakeMapper = new Mapper(new
MapperProvider().GetMapperConfig());
- 1 回答
- 0 关注
- 185 浏览
添加回答
举报
0/150
提交
取消