コード例 #1
0
        public void AddServiceWithTServiceOnly()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService <Foo>(ServiceLifetime.Transient);
            IServiceProvider container = builder.BuildContainer();

            Assert.NotNull(container.GetService <Foo>());
        }
コード例 #2
0
        public void AddServiceWithTServiceAndTImplementationFactory()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService <IFoo>(ServiceLifetime.Transient, sp => new Foo());
            IServiceProvider container = builder.BuildContainer();

            Assert.NotNull(container.GetService <IFoo>());
        }
コード例 #3
0
        public void AddServiceWithServiceType()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService(ServiceLifetime.Transient, typeof(Foo));
            IServiceProvider container = builder.BuildContainer();

            Assert.NotNull(container.GetService <Foo>());
        }
コード例 #4
0
        public void GetNonExistingRequiredServiceThrows()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService(ServiceLifetime.Transient, typeof(Foo));
            IServiceProvider container = builder.BuildContainer();

            Assert.Throws(typeof(ODataException), () => container.GetRequiredService <IFoo>());
        }
コード例 #5
0
        public void GetRequiredServiceNonGeneric()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService(ServiceLifetime.Transient, typeof(Foo));
            IServiceProvider container = builder.BuildContainer();

            Assert.NotNull(container.GetRequiredService(typeof(Foo)));
        }
コード例 #6
0
        public void GetNonExistingServiceGeneric()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService(ServiceLifetime.Transient, typeof(Foo));
            IServiceProvider container = builder.BuildContainer();

            Assert.Null(container.GetService <IFoo>());
        }
コード例 #7
0
        public void GetServicesGeneric()
        {
            IContainerBuilder builder = new TestContainerBuilder();

            builder.AddService <IFoo, Foo>(ServiceLifetime.Transient);
            builder.AddService <IFoo, Bar>(ServiceLifetime.Transient);
            IServiceProvider container = builder.BuildContainer();

            Assert.Equal(2, container.GetServices <IFoo>().Count());
        }
コード例 #8
0
        /// <summary>
        /// Gets the shared DI container
        /// </summary>
        /// <returns>Instance of the DI container</returns>
        private static IServiceProvider GetSharedContainer()
        {
            if (container == null)
            {
                var builder = new TestContainerBuilder();
                builder.AddDefaultODataServices();
                container = builder.BuildContainer();
            }

            return(container);
        }
コード例 #9
0
        public void Singleton_references_returned_by_ResolveAll_and_Resolve_should_be_same_instance()
        {
            using (var builder = TestContainerBuilder.ConstructBuilder())
            {
                builder.Configure(typeof(AnotherSingletonComponent), DependencyLifecycle.SingleInstance);
                builder.Configure(typeof(SingletonComponent), DependencyLifecycle.SingleInstance);

                var resolveResult    = builder.Build(typeof(SingletonComponent));
                var resolveAllResult = builder.BuildAll(typeof(ISingletonComponent)).OfType <SingletonComponent>().Single();

                Assert.AreSame(resolveResult, resolveAllResult);
            }
        }
コード例 #10
0
        private ITestContainerBuilder GetTestContainerBuilder()
        {
            var builder = new TestContainerBuilder
            {
                AdditionalTypesRegistration = containerBuilder =>
                {
                    UnitTestBaseRegistrator.RegisterTestComponents <TDialog>(containerBuilder, this.From);

                    this.RegisterAdditionalTypes(containerBuilder);
                }
            };

            return(builder);
        }
コード例 #11
0
        private ITestContainerBuilder GetTestContainerBuilder()
        {
            var builder = new TestContainerBuilder
            {
                AdditionalTypesRegistration = containerBuilder =>
                {
                    UnitTestBaseRegistrator.RegisterTestComponents(containerBuilder, this.From, this.registerDialogInstanceFunc);

                    this.RegisterAdditionalTypes(containerBuilder);
                },
            };

            return(builder);
        }
        public void WorkflowWriter_Service_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            WorkflowWriterTestServiceWriter.ResetCount();
            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IWorkflowWriterTestService>();
                service.Method1();
            }

            Assert.AreEqual(expected: 1, actual: WorkflowWriterTestServiceWriter.GetPreInvocationCallCount());
            Assert.AreEqual(expected: 1, actual: WorkflowWriterTestServiceWriter.GetPostInvocationCallCount());
        }
コード例 #13
0
        public void Validation_Service_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            ValidationTestServiceValidator.ResetCount();
            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IValidationTestService>();
                service.Method1();
            }

            Assert.AreEqual(expected: 1, actual: ValidationTestServiceValidator.GetInputParametersCallCount());
            Assert.AreEqual(expected: 1, actual: ValidationTestServiceValidator.GetResultCallCount());
        }
コード例 #14
0
        public void Logging_Elevated_To_Warn_Service_Test()
        {
            var applicationConfiguration =
                new ApplicationConfiguration("appsettings.json");

            applicationConfiguration.GenerateConfiguration();

            var containerBuilder = new TestContainerBuilder(applicationConfiguration);
            var autofacContainer = containerBuilder.BuildContainer();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <ILoggingElevatedToWarn>();
                service.Method1("Input Warn Value");
            }
        }
コード例 #15
0
        public void Request_Response_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            MediatRReceiverService.ResetCount();
            RequestResponseService.ResetCount();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IMediatRReceiverService>();
                service.RunRequest();
            }

            Assert.AreEqual(expected: 1, actual: RequestResponseService.GetCallCount());
            Assert.AreEqual(expected: 1, actual: MediatRReceiverService.GetCallCount());
        }
コード例 #16
0
        public void Notification_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            MediatRReceiverService.ResetCount();
            NotificationService.ResetCount();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IMediatRReceiverService>();
                service.RunNotification();
            }

            Assert.AreEqual(expected: 1, actual: NotificationService.GetCallCount());
            Assert.AreEqual(expected: 1, actual: MediatRReceiverService.GetCallCount());
        }
コード例 #17
0
        public void Logging_Service_Exception_Test()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            LoggingTestExceptionServiceLogger.ResetCount();
            LoggingTestExceptionService.ResetCount();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <ILoggingTestExceptionService>();
                service.Method1();
                service.Method1();
            }

            Assert.AreEqual(expected: 2, actual: LoggingTestExceptionService.GetCallCount());
            Assert.AreEqual(expected: 1, actual: LoggingTestExceptionServiceLogger.GetPreInvocationCallCount());
            Assert.AreEqual(expected: 1, actual: LoggingTestExceptionServiceLogger.GetPostInvocationCallCount());
        }
コード例 #18
0
        public void Not_Executed_In_Test_Mode()
        {
            var containerBuilder = new TestContainerBuilder();
            var autofacContainer = containerBuilder.BuildContainer();

            Service.ResetCount();
            LiveService.ResetCount();

            using (var scope = autofacContainer.BeginLifetimeScope())
            {
                var service = scope.Resolve <IService>();
                service.RunService();
            }

            // Service will be called once
            Assert.AreEqual(expected: 1, actual: Service.GetCallCount());

            // Live service implementation should not be called.
            Assert.AreEqual(expected: 0, actual: LiveService.GetCallCount());
        }
        private ITestContainerBuilder GetTestContainerBuilder()
        {
            var builder = new TestContainerBuilder
            {
                AdditionalTypesRegistration = containerBuilder =>
                {
                    var luisServiceMock = this.GetLuisServiceMock();

                    containerBuilder
                    .Register(c => luisServiceMock.Object)
                    .Keyed <ILuisService>(FiberModule.Key_DoNotSerialize)
                    .As <ILuisService>()
                    .SingleInstance();

                    UnitTestBaseRegistrator.RegisterTestComponents <TDialog>(containerBuilder, this.From);

                    this.RegisterAdditionalTypes(containerBuilder);
                }
            };

            return(builder);
        }
コード例 #20
0
        public static void InitSimpleServiceTest(TestContext testContext)
        {
            var containerBuilder = new TestContainerBuilder();

            autofacContainer = containerBuilder.BuildContainer();
        }