コード例 #1
0
        public void ShouldRegisterWithConventionAndWithRegistration()
        {
            //Arrange
            var assembly = Assembly.GetAssembly(typeof(TestUnityRegistration));

            A.CallTo(() => registrationHelper.GetAssemblies())
            .Returns(new List <Assembly> {
                assembly
            });
            A.CallTo(() => registrationHelper.GetTypesImplementing <IUnityRegistration>(assembly))
            .Returns(new List <Type> {
                typeof(TestUnityRegistration)
            });
            var containerExtension = new UnityExtension(registrationHelper, options);

            A.CallTo(() => options.AutoRegistration).Returns(true);

            //Act
            containerExtension.Run();

            //Assert
            A.CallTo(() => registrationHelper.GetAssemblies()).MustHaveHappened();
            A.CallTo(() => registrationHelper.GetTypesImplementing <IUnityRegistration>(assembly)).MustHaveHappened();
            Assert.IsNotNull(containerExtension.Resolve <IRegistrationHelper>());
            Assert.IsNotNull(containerExtension.Resolve <UnityExtension>());
            Assert.IsNotNull(containerExtension.Resolve <IRegisteredByConvention>());
        }
コード例 #2
0
        public void ShouldInvokeTheRegisterMethodOfAllIUnityRegistrationTypes()
        {
            //Arrange
            var assembly = Assembly.GetAssembly(typeof(TestUnityRegistration));

            A.CallTo(() => registrationHelper.GetAssemblies())
            .Returns(new List <Assembly> {
                assembly
            });
            A.CallTo(() => registrationHelper.GetTypesImplementing <IUnityRegistration>(assembly))
            .Returns(new List <Type> {
                typeof(TestUnityRegistration)
            });
            var containerExtension = new UnityExtension(registrationHelper, options);

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

            //Assert
            A.CallTo(() => registrationHelper.GetAssemblies()).MustHaveHappened();
            A.CallTo(() => registrationHelper.GetTypesImplementing <IUnityRegistration>(assembly)).MustHaveHappened();
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(UnityExtension));
        }
コード例 #3
0
        public void ShouldThrowNoContainerExceptionWhenResolvingSimpleTypeBeforeInitializingTheContainer()
        {
            //Arrange
            var containerExtension = new UnityExtension(registrationHelper, options);

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

            //Assert
            Assert.AreEqual(NoContainerException.DefaultMessage, result.Message);
        }
コード例 #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);
        }
コード例 #5
0
        public void ShouldInvokeTheRegisterMethodOfAllIBootstrapperRegistrationTypes()
        {
            //Arrange
            var assembly = Assembly.GetAssembly(typeof(AutoMapperRegistration));

            A.CallTo(() => registrationHelper.GetAssemblies())
            .Returns(new List <Assembly> {
                assembly
            });
            A.CallTo(() => registrationHelper.GetTypesImplementing <IBootstrapperRegistration>(assembly))
            .Returns(new List <Type> {
                typeof(AutoMapperRegistration)
            });
            var containerExtension = new UnityExtension(registrationHelper, options);

            //Act
            containerExtension.Run();

            //Assert
            A.CallTo(() => registrationHelper.GetAssemblies()).MustHaveHappened();
            A.CallTo(() => registrationHelper.GetTypesImplementing <IBootstrapperRegistration>(assembly)).MustHaveHappened();
            Assert.IsNotNull(containerExtension.Resolve <IProfileExpression>());
        }
コード例 #6
0
        public void Run_WhenInvokedAndAutoMapperExtensionIsLoaded_ShouldRegisterMapperAsSingelton()
        {
            //Arrange
            var assembly = Assembly.GetAssembly(typeof(AutoMapperRegistration));

            A.CallTo(() => registrationHelper.GetAssemblies())
            .Returns(new List <Assembly> {
                assembly
            });
            A.CallTo(() => registrationHelper.GetTypesImplementing <IBootstrapperRegistration>(assembly))
            .Returns(new List <Type> {
                typeof(AutoMapperRegistration)
            });
            var containerExtension = new UnityExtension(registrationHelper, options);

            //Act
            containerExtension.Run();

            //Assert
            Assert.AreSame(Mapper.Configuration, containerExtension.Resolve <IProfileExpression>());
            Assert.AreSame(Mapper.Engine, containerExtension.Resolve <IMappingEngine>());
            Assert.AreSame(containerExtension.Resolve <IProfileExpression>(), containerExtension.Resolve <IProfileExpression>());
            Assert.AreSame(containerExtension.Resolve <IMappingEngine>(), containerExtension.Resolve <IMappingEngine>());
        }