Ejemplo n.º 1
0
        public void ShouldRegisterWithTargetType()
        {
            //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 UnityContainer();
            var containerExtension = new UnityExtension(registrationHelper, options);

            containerExtension.InitializeContainer(container);

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

            //Assert
            A.CallTo(() => registrationHelper.GetAssemblies()).MustHaveHappened();
            A.CallTo(() => registrationHelper.GetTypesImplementing <IBootstrapperAssemblyProvider>(assembly)).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));
        }
Ejemplo n.º 2
0
        public void ShouldInitializeTheContainerToTheValuePassed()
        {
            //Arrange
            var containerExtension = new UnityExtension(registrationHelper, options);
            var container          = A.Fake <IUnityContainer>();

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

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IUnityContainer));
            Assert.AreSame(result, container);
        }
Ejemplo n.º 3
0
        public void ShouldRegisterWithTargetAndImplementationType()
        {
            //Arrange
            var container          = new UnityContainer();
            var containerExtension = new UnityExtension(registrationHelper, options);

            containerExtension.InitializeContainer(container);

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

            //Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(LoadedAssemblyProvider));
        }
Ejemplo n.º 4
0
        public void ShouldResolveASingleNamedType()
        {
            //Arrange
            var containerExtension = new UnityExtension(registrationHelper, options);
            var container          = new UnityContainer();
            var instance           = new object();

            container.RegisterInstance("Name", instance);
            containerExtension.InitializeContainer(container);

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

            //Assert
            Assert.IsNotNull(result);
            Assert.AreSame(instance, result);
        }
Ejemplo n.º 5
0
        public void ShouldResolveMultipleTypes()
        {
            //Arrange
            var containerExtension = new UnityExtension(registrationHelper, options);
            var container          = new UnityContainer();
            var instance1          = new object();
            var instance2          = new object();

            container.RegisterInstance("Name1", instance1);
            container.RegisterInstance("Name2", instance2);
            containerExtension.InitializeContainer(container);

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

            //Assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Any(o => o == instance1));
            Assert.IsTrue(result.Any(o => o == instance2));
        }
Ejemplo n.º 6
0
        public void RegisterAll_WhenInvokedWithNonGenericTargetType_ShouldRegisterType()
        {
            //Arrange
            var container          = new UnityContainer();
            var containerExtension = new UnityExtension(registrationHelper, options);

            containerExtension.InitializeContainer(container);
            var thisAssembly = Assembly.GetCallingAssembly();

            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.ResolveAll <IGenericTest <object> >().First();
            var result2 = container.ResolveAll <IGenericTest <string[]> >().First();

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