Esempio n. 1
0
        public void GetInstance_RegisterOpenGenericWithRegistrationWithMissingDependency_ThrowsExpectedException()
        {
            // Arrange
            var container = ContainerFactory.New();

            // DefaultStuffDoer depends on IService<T, int> but this isn't registered.
            container.RegisterOpenGeneric(typeof(IDoStuff <>), typeof(DefaultStuffDoer <>));

            try
            {
                // Act
                container.GetInstance <IDoStuff <bool> >();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                AssertThat.ExceptionMessageContains(@"
                    There was an error in the registration of open generic type IDoStuff<T>. 
                    Failed to build a registration for type DefaultStuffDoer<Boolean>.".TrimInside(),
                                                    ex);

                AssertThat.ExceptionMessageContains(@"                                                                     
                    The constructor of type DefaultStuffDoer<Boolean> 
                    contains the parameter of type IService<Boolean, Int32> with name 'service' that 
                    is not registered.".TrimInside(),
                                                    ex);
            }
        }
        public void ConstructorInjectionBehavior_CustomBehaviorThatReturnsNull_ThrowsExpressiveException()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.Options.ConstructorInjectionBehavior = new FakeConstructorInjectionBehavior
            {
                ExpressionToReturnFromBuildParameterExpression = null
            };

            container.Register <IUserRepository, SqlUserRepository>();

            try
            {
                // Act
                // RealUserService depends on IUserRepository
                container.GetInstance <RealUserService>();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                AssertThat.ExceptionMessageContains(
                    "FakeConstructorInjectionBehavior that was registered through " +
                    "Container.Options.ConstructorInjectionBehavior returned a null reference", ex);
                AssertThat.ExceptionMessageContains(
                    "argument of type IUserRepository with name 'repository' from the constructor of type " +
                    "RealUserService", ex);
            }
        }
Esempio n. 3
0
        public void CreateRegistrationTService_Always_ThrowsException()
        {
            // Arrange
            var container = ContainerFactory.New();

            try
            {
                // Act
                Lifestyle.Unknown.CreateRegistration <IDisposable>(() => null, container);

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (InvalidOperationException ex)
            {
                AssertThat.ExceptionMessageContains(
                    "The unknown lifestyle does not allow creation of registrations.", ex);
            }
        }
        public void InjectingAllProperties_OnPrivateTypeWithPrivateSetterPropertyInSilverlight_FailsWithDescriptiveMessage()
        {
            // Arrange
            var container = CreateContainerThatInjectsAllProperties();

            container.Register <ITimeProvider, RealTimeProvider>(Lifestyle.Singleton);

            try
            {
                // Act
                container.GetInstance <PrivateServiceWithPrivateSetPropertyDependency <ITimeProvider> >();
            }
            catch (Exception ex)
            {
                AssertThat.ExceptionMessageContains(@"
                    The security restrictions of your application's sandbox do not permit the injection of 
                    one of its properties.".TrimInside(), ex);
            }
        }
        public void GetInstance_OnInternalTypeRegisteredAsOpenGeneric_ThrowsDescriptiveExceptionMessage()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.Register(typeof(IEventHandler <>), typeof(InternalEventHandler <>));

            try
            {
                // Act
                container.GetInstance <IEventHandler <int> >();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                AssertThat.ExceptionMessageContains("InternalEventHandler<Int32>", ex);
                AssertThat.ExceptionMessageContains("The security restrictions of your application's " +
                                                    "sandbox do not permit the creation of this type.", ex);
            }
        }
        public void GetInstance_WithoutLifetimeScope_ThrowsExpectedException()
        {
            // Arrange
            var container = new Container();

            container.Register <ICommand, ConcreteCommand>(new ThreadScopedLifestyle());

            try
            {
                // Act
                var firstInstance = container.GetInstance <ICommand>();

                // Assert
                Assert.Fail("Exception expected.");
            }
            catch (ActivationException ex)
            {
                AssertThat.ExceptionMessageContains(@"
                    ConcreteCommand is registered as 'Thread Scoped' lifestyle, but the instance is
                    requested outside the context of an active (Thread Scoped) scope."
                                                    .TrimInside(),
                                                    ex);
            }
        }