コード例 #1
0
        public void NativeContainerIsRegistered()
        {
            var    container = new ContainerExtension();
            object resolved  = container.Resolve <INativeContainer>();

            Assert.Same(container.Instance, resolved);
        }
コード例 #2
0
        public void RegisterManyRegistersServicesAsTransients()
        {
            var container = new ContainerExtension();

            container.RegisterMany <AggregateService>();
            Assert.NotSame(container.Resolve <IServiceA>(), container.Resolve <IServiceA>());
        }
コード例 #3
0
        public void IContainerExtensionIsRegistered()
        {
            var    container = new ContainerExtension();
            object resolved  = container.Resolve <IContainerExtension>();

            Assert.Same(container, resolved);
        }
コード例 #4
0
        public void ThrowsContainerResolutionExceptionForUnregisteredNamedPage()
        {
            var container = new ContainerExtension();
            var ex        = Record.Exception(() => container.Resolve <object>("missing"));

            Assert.NotNull(ex);
            Assert.IsType <ContainerResolutionException>(ex);
        }
コード例 #5
0
        public void ThrowsContainerResolutionExceptionForUnregisteredService()
        {
            var container = new ContainerExtension();
            var ex        = Record.Exception(() => container.Resolve <IServiceMock>());

            Assert.NotNull(ex);
            Assert.IsType <ContainerResolutionException>(ex);
        }
コード例 #6
0
        public void AutomaticTransientResolutionOfConcreteType()
        {
            var container = new ContainerExtension();
            var resolved1 = container.Resolve <ServiceMock>();
            var resolved2 = container.Resolve <ServiceMock>();

            Assert.NotNull(resolved1);
            Assert.NotSame(resolved1, resolved2);
        }
コード例 #7
0
        public void LocatesImplementationType()
        {
            var container = new ContainerExtension();

            container.Register <IServiceMock, ServiceMock>();
            var type = container.GetRegistrationType(typeof(IServiceMock));

            Assert.NotNull(type);
            Assert.Equal(typeof(ServiceMock), type);
        }
コード例 #8
0
        public void RegisterManySingletonRegistersAllInterfacesByDefault()
        {
            var container = new ContainerExtension();

            container.RegisterManySingleton <AggregateService>();

            Assert.IsType <AggregateService>(container.Resolve <IServiceA>());
            Assert.IsType <AggregateService>(container.Resolve <IServiceB>());
            Assert.IsType <AggregateService>(container.Resolve <IServiceC>());
        }
コード例 #9
0
        public void RegisterNamedInstanceReturnsSameInstance()
        {
            var instance  = new ServiceMock();
            var container = new ContainerExtension();

            container.RegisterInstance <object>(instance, "Test");
            var resolved = container.Resolve <object>("Test");

            Assert.Same(instance, resolved);
        }
コード例 #10
0
        public void LocatesImplementationTypeForNamedService()
        {
            var container = new ContainerExtension();

            container.RegisterForNavigation <ViewAMock>();
            var type = container.GetRegistrationType(nameof(ViewAMock));

            Assert.NotNull(type);
            Assert.Equal(typeof(ViewAMock), type);
        }
コード例 #11
0
        public void RegisterNamedConcreteTypeCreatesTransient()
        {
            var container = new ContainerExtension();

            container.Register <ServiceMock>("Test");
            var resolved1 = container.Resolve <ServiceMock>("Test");
            var resolved2 = container.Resolve <ServiceMock>("Test");

            Assert.NotNull(resolved1);
            Assert.NotSame(resolved1, resolved2);
        }
コード例 #12
0
        public void RegisterSingletonConcreteTypeCreatesSingleton()
        {
            var container = new ContainerExtension();

            container.RegisterSingleton <ServiceMock>();
            var resolved1 = container.Resolve <ServiceMock>();
            var resolved2 = container.Resolve <ServiceMock>();

            Assert.NotNull(resolved1);
            Assert.Same(resolved1, resolved2);
        }
コード例 #13
0
        public void RegisterManySingletonOnlyRegistersSpecifiedInterfaces()
        {
            var container = new ContainerExtension();

            container.RegisterManySingleton <AggregateService>(typeof(IServiceA), typeof(IServiceB));

            Assert.IsType <AggregateService>(container.Resolve <IServiceA>());
            Assert.IsType <AggregateService>(container.Resolve <IServiceB>());
            var ex = Record.Exception(() => container.Resolve <IServiceC>());

            Assert.NotNull(ex);
        }
コード例 #14
0
        public void RegisterManySingletonUsesSharedInstanceForAllServices()
        {
            var container = new ContainerExtension();

            container.RegisterManySingleton <AggregateService>();

            var serviceA = container.Resolve <IServiceA>();
            var serviceB = container.Resolve <IServiceB>();
            var serviceC = container.Resolve <IServiceC>();

            Assert.Same(serviceA, serviceB);
            Assert.Same(serviceB, serviceC);
        }
コード例 #15
0
        public void RegisterServiceMappingCreatesTransient()
        {
            var container = new ContainerExtension();

            container.Register <IServiceMock, ServiceMock>();
            var resolved1 = container.Resolve <IServiceMock>();
            var resolved2 = container.Resolve <IServiceMock>();

            Assert.NotNull(resolved1);
            Assert.NotNull(resolved2);
            Assert.IsType <ServiceMock>(resolved1);
            Assert.IsType <ServiceMock>(resolved2);
            Assert.NotSame(resolved1, resolved2);
        }
コード例 #16
0
        public void RegisterSingletonWithFactoryAndContainerProvider()
        {
            var container = new ContainerExtension();

            container.Register <IServiceMock, ServiceMock>(nameof(ServiceMock));
            container.RegisterSingleton <IServiceMock>(CreateServiceWithContainerProvider);
            var resolved = container.Resolve <IServiceMock>();

            Assert.NotNull(resolved);
            Assert.IsType <ServiceMock>(resolved);
            var serviceMock = resolved as ServiceMock;
            var resolved2   = container.Resolve <IServiceMock>();

            Assert.Same(resolved, resolved2);
        }
コード例 #17
0
        public void LocatesUnregisteredServiceWithMissingRegistration()
        {
            ContainerLocator.ResetContainer();
            var container = new ContainerExtension();

            ContainerLocator.SetContainerExtension(() => container);

            var ex = Record.Exception(() => container.Resolve <ConstructorArgumentViewModel>());

            Assert.IsType <ContainerResolutionException>(ex);
            var cre    = ex as ContainerResolutionException;
            var errors = cre.GetErrors();

            Assert.Contains(errors, x => x.Value is ContainerResolutionException innerCre && innerCre.ServiceType == typeof(IServiceMock) && innerCre.Message == ContainerResolutionException.MissingRegistration);
        }
コード例 #18
0
        public void LocatesUnregisteredServiceType()
        {
            ContainerLocator.ResetContainer();
            var container = new ContainerExtension();

            ContainerLocator.SetContainerExtension(() => container);

            var ex = Record.Exception(() => container.Resolve <ConstructorArgumentViewModel>());

            Assert.IsType <ContainerResolutionException>(ex);
            var cre    = ex as ContainerResolutionException;
            var errors = cre.GetErrors();

            Assert.Contains(typeof(IServiceMock), errors.Types);
        }
コード例 #19
0
        public void RegisterSingletonSupportsFuncInjection()
        {
            var container = new ContainerExtension();

            container.RegisterSingleton <IServiceMock, ServiceMock>();
            var func = container.Resolve <Func <IServiceMock> >();

            Assert.NotNull(func);
            var instance = func();

            Assert.IsType <ServiceMock>(instance);

            var func2 = container.Resolve <Func <IServiceMock> >();

            Assert.Same(instance, func2());
        }
コード例 #20
0
        public void RegisterSingletonWithFactory()
        {
            var container = new ContainerExtension();

            container.RegisterSingleton <IServiceMock>(CreateService);
            var resolved = container.Resolve <IServiceMock>();

            Assert.NotNull(resolved);
            Assert.IsType <ServiceMock>(resolved);
            var serviceMock = resolved as ServiceMock;

            Assert.Equal("Created through a factory", serviceMock.SomeProperty);
            var resolved2 = container.Resolve <IServiceMock>();

            Assert.Same(resolved, resolved2);
        }
コード例 #21
0
        public void RegisterSingletonNamedServiceMappingCreatesSingleton()
        {
            var container = new ContainerExtension();

            container.RegisterSingleton <IServiceMock, ServiceMock>("Test");
            var resolved1 = container.Resolve <IServiceMock>("Test");
            var resolved2 = container.Resolve <IServiceMock>("Test");
            var ex        = Record.Exception(() => container.Resolve <IServiceMock>());

            Assert.NotNull(ex);
            Assert.NotNull(resolved1);
            Assert.NotNull(resolved2);
            Assert.IsType <ServiceMock>(resolved1);
            Assert.IsType <ServiceMock>(resolved2);
            Assert.Same(resolved1, resolved2);
        }
コード例 #22
0
        public void RegisterScopedConcreteTypeCreatesScoped()
        {
            var container = new ContainerExtension();

            container.RegisterScoped <ServiceMock>();
            var resolved1 = container.Resolve <ServiceMock>();
            var resolved2 = container.Resolve <ServiceMock>();

            Assert.NotNull(resolved1);
            Assert.Same(resolved1, resolved2);

            container.CreateScope();
            var resolved3 = container.Resolve <ServiceMock>();

            Assert.NotSame(resolved1, resolved3);
        }
コード例 #23
0
        public void GetErrorsLocatesTargetInvocationException()
        {
            ContainerLocator.ResetContainer();
            var container = new ContainerExtension();

            ContainerLocator.SetContainerExtension(() => container);
            container.Register <object, BadView>("BadView");

            var ex = Record.Exception(() => container.Resolve <object>("BadView"));

            Assert.IsType <ContainerResolutionException>(ex);
            var cre    = ex as ContainerResolutionException;
            var errors = cre.GetErrors();

            Assert.Contains(errors, x => x.Value is TargetInvocationException);
        }
コード例 #24
0
        public void GetErrorsLocatesIssueWithBadView()
        {
            ContainerLocator.ResetContainer();
            var container = new ContainerExtension();

            ContainerLocator.SetContainerExtension(() => container);
            container.Register <object, BadView>("BadView");

            var ex = Record.Exception(() => container.Resolve <object>("BadView"));

            Assert.IsType <ContainerResolutionException>(ex);
            var cre    = ex as ContainerResolutionException;
            var errors = cre.GetErrors();

            Assert.Contains(typeof(BadView), errors.Types);
        }
コード例 #25
0
        public void RegisterSingletonSupportsLazyInjection()
        {
            var container = new ContainerExtension();

            container.RegisterSingleton <IServiceMock, ServiceMock>();
            var lazy = container.Resolve <Lazy <IServiceMock> >();

            Assert.NotNull(lazy);
            Assert.False(lazy.IsValueCreated);
            var instance = lazy.Value;

            Assert.IsType <ServiceMock>(instance);

            var lazy2 = container.Resolve <Lazy <IServiceMock> >();

            Assert.Same(instance, lazy2.Value);
        }
コード例 #26
0
        public void GetErrorsDoesNotThrowException()
        {
            ContainerLocator.ResetContainer();
            var container = new ContainerExtension();

            ContainerLocator.SetContainerExtension(() => container);
            container.Register <object, BadView>("BadView");

            var ex = Record.Exception(() => container.Resolve <object>("BadView"));

            Assert.IsType <ContainerResolutionException>(ex);
            var cre = ex as ContainerResolutionException;
            ContainerResolutionErrorCollection errors = null;
            var ex2 = Record.Exception(() => errors = cre.GetErrors());

            Assert.Null(ex2);
        }
コード例 #27
0
        public void RegisterScopedWithFactoryAndContainerProvider()
        {
            var container = new ContainerExtension();

            container.Register <IServiceMock, ServiceMock>(nameof(ServiceMock));
            container.RegisterScoped <IServiceMock>(CreateServiceWithContainerProvider);
            var resolved1 = container.Resolve <IServiceMock>();

            Assert.NotNull(resolved1);
            Assert.IsType <ServiceMock>(resolved1);

            var resolved2 = container.Resolve <IServiceMock>();

            Assert.Same(resolved1, resolved2);
            container.CreateScope();
            var resolved3 = container.Resolve <IServiceMock>();

            Assert.NotSame(resolved2, resolved3);
        }
コード例 #28
0
        public void RegisterScopedWithFactory()
        {
            var container = new ContainerExtension();

            container.RegisterScoped <IServiceMock>(CreateService);
            var resolved1 = container.Resolve <IServiceMock>();

            Assert.NotNull(resolved1);
            Assert.IsType <ServiceMock>(resolved1);
            var serviceMock = resolved1 as ServiceMock;

            Assert.Equal("Created through a factory", serviceMock.SomeProperty);

            var resolved2 = container.Resolve <IServiceMock>();

            Assert.Same(resolved1, resolved2);
            container.CreateScope();
            var resolved3 = container.Resolve <IServiceMock>();

            Assert.NotSame(resolved2, resolved3);
        }