public void AddServiceWithTServiceOnly()
        {
            IContainerBuilder builder = new TestContainerBuilder();

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

            Assert.NotNull(container.GetService <Foo>());
        }
        public void AddServiceWithTServiceAndTImplementationFactory()
        {
            IContainerBuilder builder = new TestContainerBuilder();

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

            Assert.NotNull(container.GetService <IFoo>());
        }
        public void AddServiceWithServiceType()
        {
            IContainerBuilder builder = new TestContainerBuilder();

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

            Assert.NotNull(container.GetService <Foo>());
        }
        public void GetNonExistingRequiredServiceThrows()
        {
            IContainerBuilder builder = new TestContainerBuilder();

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

            Assert.Throws(typeof(ODataException), () => container.GetRequiredService <IFoo>());
        }
        public void GetRequiredServiceNonGeneric()
        {
            IContainerBuilder builder = new TestContainerBuilder();

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

            Assert.NotNull(container.GetRequiredService(typeof(Foo)));
        }
        public void GetNonExistingServiceGeneric()
        {
            IContainerBuilder builder = new TestContainerBuilder();

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

            Assert.Null(container.GetService <IFoo>());
        }
        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());
        }
Beispiel #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);
        }
        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);
            }
        }
Beispiel #10
0
        private ITestContainerBuilder GetTestContainerBuilder()
        {
            var builder = new TestContainerBuilder
            {
                AdditionalTypesRegistration = containerBuilder =>
                {
                    UnitTestBaseRegistrator.RegisterTestComponents <TDialog>(containerBuilder, this.From);

                    this.RegisterAdditionalTypes(containerBuilder);
                }
            };

            return(builder);
        }
Beispiel #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());
        }
        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());
        }
        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");
            }
        }
Beispiel #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());
        }
Beispiel #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());
        }
        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());
        }
Beispiel #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);
        }
Beispiel #20
0
        public static void InitSimpleServiceTest(TestContext testContext)
        {
            var containerBuilder = new TestContainerBuilder();

            autofacContainer = containerBuilder.BuildContainer();
        }