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 に答える