在集成测试中使用IMapper

Don*_*ain 3 integration ninject xunit automapper

正如标题所说,我正在编写集成测试,并希望测试和初始化IMapper对象作为这些测试的一部分.

我使用XUnit作为测试框架,使用Ninject来处理依赖注入.

我习惯使用静态AutoMapper方法,并转而使用IMapper接口,这是我的一个存储库构造函数中的参数.

我有一个继承NinjectModule基类的DependencyRegistrar类.

我的加载方法如下:

public override void Load()
    {
        var automapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile<AutoMapperProfile>(); });
        automapperConfig.AssertConfigurationIsValid();
        var mapper = automapperConfig.CreateMapper();

        Bind<IMapper>().ToConstant(mapper).InSingletonScope();
        Bind<IUserManager>().To<UserManager>().InTransientScope();
        Bind<IUserRepository>().To<UserRepository>().InTransientScope();
    }
Run Code Online (Sandbox Code Playgroud)

该模块由API项目中的NinjectWebCommon类加载,但显然在集成测试期间不会加载.

ConfigurationSettingsEntity configurationSettings = new ConfigurationSettingsEntity(ConfigurationManager.AppSettings);
        var modules = new INinjectModule[] { new DependencyResolution.DependencyRegistrar(configurationSettings) };
        var kernel = new StandardKernel(modules);
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,加载真正的映射器实现的最佳方法是什么,以便在构建集成测试时我可以将该映射器传递到我的存储库的构造函数中?

UserRepository tester = new UserRepository(mapper);
Run Code Online (Sandbox Code Playgroud)

Ric*_*ard 6

绝对值得努力摆脱使用静态Mapper - 未来的测试会更容易!

在我的单元测试中,我使用如下代码:

var config = new MapperConfiguration(cfg => { 
    cfg.AddProfile<MyProfile>();
});
IMapper mapper = config.CreateMapper();
Run Code Online (Sandbox Code Playgroud)

在每个测试套件中,我手动指定这些测试所需的配置文件.