3

对于初学者,我正在使用这个模块:

    public class AutoMapperModule : NinjectModule
{
    public override void Load()
    {
        Bind<ITypeMapFactory>().To<TypeMapFactory>();
        foreach (var mapper in MapperRegistry.AllMappers())
        {
            Bind<IObjectMapper>().ToConstant(mapper);
        }

        Bind<AutoMapper.ConfigurationStore>().ToSelf().InSingletonScope().WithConstructorArgument("mappers", ctx => ctx.Kernel.GetAll<IObjectMapper>());
        Bind<IConfiguration>().ToMethod(ctx => ctx.Kernel.Get<AutoMapper.ConfigurationStore>());
        Bind<IConfigurationProvider>().ToMethod(ctx => ctx.Kernel.Get<AutoMapper.ConfigurationStore>());
        Bind<IMappingEngine>().To<MappingEngine>();
    }
}

我的所有地图都有一个引导程序类

        public static void Configure(IKernel kernel)
    {
        Mapper.Initialize(map => map.ConstructServicesUsing(t => kernel.Get(t)));
    }

我有访问数据库并需要注入存储库的解析器。它按原样工作,但我不知道如何让它与单元测试和 IMappingEngine 一起工作。

        public HomeController(IMappingEngine mappingEngine)
    {
        _mappingEngine = mappingEngine;
    }

_mappingEngine.Map 抛出异常,因为不存在地图。Mapper.Map 有效。

我错过了什么?如何让我的引导程序使用单元测试,以便解析器中的存储库使用假/模拟存储库?

4

1 回答 1

1

尝试更改映射的绑定。

Bind<IMappingEngine>().ToMethod(ctx => Mapper.Engine);
于 2012-12-18T10:27:16.620 回答