猿问

一种在 ASP.NET Core 中的集成测试,使用 EF 和 AutoMapper

我正在尝试通过包含 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);

我有意进行集成测试,因为我希望我的测试能够将数据发布到控制器并测试正确的数据是否最终出现在正确的表中。


忽然笑
浏览 186回答 1
1回答

摇曳的蔷薇

创建映射器提供程序public class MapperProvider{&nbsp; &nbsp; public MapperProvider() {&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; public MapperConfiguration GetMapperConfig()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var mce = new MapperConfigurationExpression();&nbsp; &nbsp; &nbsp; &nbsp; mce.AddProfile<AutoMapperConfig>();&nbsp; &nbsp; &nbsp; &nbsp; var mc = new MapperConfiguration(mce);&nbsp; &nbsp; &nbsp; &nbsp; return mc;&nbsp; &nbsp; }}注册映射配置AutoMapper.IMapper fakeMapper = new Mapper(new&nbsp;&nbsp; &nbsp; MapperProvider().GetMapperConfig());
随时随地看视频慕课网APP
我要回答