Пример #1
0
        public void RegisterAll_WhenInvokedWithAGenericType_ShouldRegisterAllTypesThatImplementTheGenericType()
        {
            //Arrange
            var assembly = Assembly.GetAssembly(typeof(RegistrationHelper));

            A.CallTo(() => registrationHelper.GetAssemblies()).Returns(new List <Assembly> {
                assembly
            });
            A.CallTo(() => registrationHelper.GetTypesImplementing <IBootstrapperAssemblyProvider>(assembly))
            .Returns(new List <Type> {
                typeof(LoadedAssemblyProvider), typeof(ReferencedAssemblyProvider)
            });
            var container          = new Container();
            var containerExtension = new SimpleInjectorExtension(registrationHelper, options);

            containerExtension.InitializeContainer(container);

            //Act
            containerExtension.RegisterAll <IBootstrapperAssemblyProvider>();
            var result = container.GetAllInstances <IBootstrapperAssemblyProvider>().ToList();

            //Assert
            A.CallTo(() => registrationHelper.GetAssemblies()).MustHaveHappened();
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IEnumerable <IBootstrapperAssemblyProvider>));
            Assert.IsTrue(result.Any());
            Assert.IsTrue(result.Any(c => c is LoadedAssemblyProvider));
            Assert.IsTrue(result.Any(c => c is ReferencedAssemblyProvider));
        }
Пример #2
0
        public void InitializeContainer_WhenInvoked_ShouldSetTheContainerPropertyToTheContainerProvided()
        {
            //Arrange
            var containerExtension = new SimpleInjectorExtension(registrationHelper, options);
            var container          = A.Fake <Container>();

            //Act
            containerExtension.InitializeContainer(container);
            var result = containerExtension.Container;

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(Container));
            Assert.AreSame(result, container);
        }
Пример #3
0
        public void Register_WhenInvokedWithGenericTargetAndImplementationType_ShouldRegisterType()
        {
            //Arrange
            var container          = new Container();
            var containerExtension = new SimpleInjectorExtension(registrationHelper, options);

            containerExtension.InitializeContainer(container);

            //Act
            containerExtension.Register <IBootstrapperAssemblyProvider, LoadedAssemblyProvider>();
            var result = container.GetInstance <IBootstrapperAssemblyProvider>();

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(LoadedAssemblyProvider));
        }
Пример #4
0
        public void Register_WhenInvokedWithGenericTypeAndInstance_ShouldRegisterInstance()
        {
            //Arrange
            var container          = new Container();
            var containerExtension = new SimpleInjectorExtension(registrationHelper, options);

            containerExtension.InitializeContainer(container);

            //Act
            containerExtension.Register <IBootstrapperContainerExtension>(containerExtension);
            var result = container.GetInstance <IBootstrapperContainerExtension>();

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(SimpleInjectorExtension));
            Assert.AreSame(containerExtension, result);
        }
Пример #5
0
        public void Resolve_WhenInvokedWithAGenericType_ShouldResolveToASingleInstance()
        {
            //Arrange
            var containerExtension = new SimpleInjectorExtension(registrationHelper, options);
            var container          = A.Fake <Container>();
            var instance           = new object();

            container.RegisterSingleton(instance);
            containerExtension.InitializeContainer(container);

            //Act
            var result = containerExtension.Resolve <object>();

            //Assert
            Assert.IsNotNull(result);
            Assert.AreSame(instance, result);
        }
Пример #6
0
        public void ResolveAll_WhenInvokedWithAGenericType_ShouldReturnAListOfInstances()
        {
            //Arrange
            var containerExtension = new SimpleInjectorExtension(registrationHelper, options);
            var container          = new Container();
            var instances          = new IBootstrapperExtension[] { new StartupTasksExtension(A.Fake <IRegistrationHelper>()), new ServiceLocatorExtension() };

            container.RegisterCollection(instances);
            containerExtension.InitializeContainer(container);

            //Act
            var result = containerExtension.ResolveAll <IBootstrapperExtension>();

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(instances.Count(), result.Count);
            Assert.IsTrue(instances.SequenceEqual(result));
        }
Пример #7
0
        public void RegisterAll_WhenInvokedWithNonGenericTargetType_ShouldRegisterType()
        {
            //Arrange
            var container          = new Container();
            var containerExtension = new SimpleInjectorExtension(registrationHelper, options);

            containerExtension.InitializeContainer(container);
            var thisAssembly = Assembly.GetAssembly(typeof(GenericTest <>));

            A.CallTo(() => registrationHelper.GetAssemblies()).Returns(new[] { thisAssembly });
            A.CallTo(() => registrationHelper.GetTypesImplementing(thisAssembly, typeof(IGenericTest <>)))
            .Returns(new[] { typeof(GenericTest <>) });

            //Act
            containerExtension.RegisterAll(typeof(IGenericTest <>));
            var result1 = container.GetInstance <IGenericTest <object> >();
            var result2 = container.GetInstance <IGenericTest <string[]> >();

            //Assert
            result1.ShouldBeOfType <GenericTest <object> >();
            result2.ShouldBeOfType <GenericTest <string[]> >();
        }