1
正如标题所说,我正在编写集成测试,并且希望测试并初始化IMapper对象作为这些测试的一部分。在集成测试中使用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();
}
该模块通过在API项目的NinjectWebCommon类加载,但很明显,在集成测试中没有加载。
ConfigurationSettingsEntity configurationSettings = new ConfigurationSettingsEntity(ConfigurationManager.AppSettings);
var modules = new INinjectModule[] { new DependencyResolution.DependencyRegistrar(configurationSettings) };
var kernel = new StandardKernel(modules);
所以,我的问题是,什么是下载真正的映射器实现,这样我可以传递到映射我的仓库的构造,当我打造出了集成测试的最佳方式?
UserRepository tester = new UserRepository(mapper);
这对加载映射器非常有用。谢谢! –