Exemplo n.º 1
0
        public void CanAMockRepositoryBeReturnedWhenAskingForBusinessLogicTest2()
        {
            //Arrange
            var myRepository = new Mock <IMyRepository>();

            myRepository.Setup(x => x.Get("Test")).Returns("I came from a mock - Test 2");
            GlobalKernel.Bind <IMyRepository>().ToConstant(myRepository.Object).WhenInCurrentTest();

            //Act
            var myBusinessLogic = GlobalKernel.Get <IMyBusinessLogic>();

            //Assert
            Assert.AreEqual("I came from a mock - Test 2 and then the business layer returned it.",
                            myBusinessLogic.DoSomething("Test"));
        }
Exemplo n.º 2
0
        public void Load()
        {
            var root = ApplicationRunTimeContext.GetProcessMainModuleDirectory();

            var builder = new ContainerBuilder();

            builder.RegisterInstance(this).AsImplementedInterfaces().SingleInstance();

            var list = new[]
            {
                Assembly.GetEntryAssembly(),
                Assembly.GetExecutingAssembly()
            }.Distinct();
            var extDir = Path.Combine(root, "extensions");

            if (Directory.Exists(extDir))
            {
                list = list.Union(Directory.GetFiles(extDir, "TOBA.*.dll", SearchOption.AllDirectories).Select(Assembly.LoadFile).ToArray());
            }

            var assemblies = list.ToArray();

            builder.RegisterAssemblyModules(assemblies);
            builder.RegisterAssemblyTypes(assemblies).
            Where(s =>
                  s.HasInterface <IExtension>() ||
                  s.HasInterface <IVerifyCodeRecognizeService>() ||
                  s.HasInterface <ISessionPresentor>() ||
                  s.HasInterface <IRequestInspector>()
                  ).
            AsImplementedInterfaces().
            SingleInstance();
            builder.RegisterAssemblyTypes(assemblies).
            Where(s =>
                  s.HasInterface <IRequireSessionInit>()
                  ).
            AsImplementedInterfaces().
            AsSelf();
            GlobalKernel = builder.Build();

            //初始化
            Extensions                 = GlobalKernel.Resolve <IExtension[]>();
            SessionPresentors          = GlobalKernel.Resolve <ISessionPresentor[]>();
            WebRequestInspectors       = GlobalKernel.Resolve <IRequestInspector[]>();
            VerifyCodeRecogniseService = GlobalKernel.Resolve <IVerifyCodeRecognizeService[]>();

            Extensions.ForEach(s => s.Connect());
        }