0

我有一个具有多个实现的 c# 接口,每个实现都在其构造函数中采用各种依赖项对象。

为了在 web api 项目上使用 LightInjector 实现 DI,我注册了一个如下所示的“Func”,它按预期工作,但无法对其进行单元测试。

有什么方法可以以可单元测试的方式编写以下代码?

 container.RegisterInstance<Func<IHandler, HandlerType, IHandler>>
        ((handler, type) =>
        {
            IHandler context = null;
            switch (type)
            {
                case HandlerType.Type0:
                    context = new Type0(orderHandler, container.GetInstance<IUtilityLogic>());
                    break;
                case HandlerType.Type1:
                    context = new Type1(orderHandler, container.GetInstance<IUtilityLogic>(), container.GetInstance<IShipmentLogic>());
                    break;
                case HandlerType.Type2:
                    context = new Type2(orderHandler);
                    break;
                case HandlerType.Type3:
                    context = new Type3(orderHandler, container.GetInstance<IUtilityLogic>());
                    break;
                case HandlerType.Type4:
                    context = new Type4(orderHandler, container.GetInstance<IUtilityLogic>(), container.GetInstance<IShipmentLogic>(), container.GetInstance<Func<string, string, ICache>>());
                    break;
                default:
                    context = null;
                    break;
            }

            return context;
        });
4

0 回答 0