public void GetInstance_SingletonThatGetsInjectedWithArrayWithTransients_ThrowsMismatchDetected()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.Collection.Append <ILogger, ConsoleLogger>(Lifestyle.Transient);
            container.RegisterSingleton <ILogger, NoncaptivatingCompositeLogger <ILogger[]> >();

            // Act
            Action action = () => container.GetInstance <ILogger>();

            // Assert
            // No special communication about iterating during the collection: this can't be
            // detected as array is not a stream and can't be intercepted.
            AssertThat.ThrowsWithExceptionMessageDoesNotContain <ActivationException>(
                ResolvingServicesFromAnInjectedCollectionMessage,
                action);

            AssertThat.ThrowsWithExceptionMessageContains <ActivationException>(
                "lifestyle mismatch has been detected",
                action);
        }
Esempio n. 2
0
        public void RegisterConcrete_CustomLifestyleSelectionBehaviorThatReturnsANullReference_ThrowsDescriptiveException()
        {
            // Arrange
            Lifestyle lifestyle = null;

            var container = new Container();

            container.Options.LifestyleSelectionBehavior = new CustomLifestyleSelectionBehavior(lifestyle);

            // Act
            Action action = () => container.Register <RealTimeProvider>();

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains <ActivationException>(@"
                The CustomLifestyleSelectionBehaviorTests.CustomLifestyleSelectionBehavior that was registered 
                through Container.Options.LifestyleSelectionBehavior returned a null reference after its 
                SelectLifestyle(Type, Type) method was supplied with values 'RealTimeProvider' for serviceType 
                and 'RealTimeProvider' for implementationType. ILifestyleSelectionBehavior.SelectLifestyle 
                implementations should never return null.
                ".TrimInside(),
                                                                                action);
        }
        public void InjectAllProperties_TypeWithStaticProperty_ThrowsExpectedException()
        {
            // Arrange
            var container = CreateContainerThatInjectsAllProperties();

            container.RegisterSingle <ITimeProvider, RealTimeProvider>();

            // Act
            Action action = () => container.GetInstance <ServiceWithStaticPropertyDependency <ITimeProvider> >();

            // Assert
            // An exception should be thrown and static properties should not be ignored. Ignoring them could
            // lead to a fragile configuration, because when a IPropertySelectionBehavior implementation
            // returns true for that given property, it would not expect it to be ignored. Take for instance
            // an custom IPropertySelectionBehavior that reacts on some [Inject] attribute to enable property
            // injection. When an application developer decorates a property with [Inject], ignoring that
            // property when it is static would be a bad thing. On the other hand, it would be as bad as trying
            // to inject into the static property.
            AssertThat.ThrowsWithExceptionMessageContains <ActivationException>(
                "Property of type ITimeProvider with name 'Dependency' can't be injected, because it is static.",
                action);
        }
Esempio n. 4
0
        public void RegisterOpenGeneric_TwoEquivalentImplementationsOfTheSameInterfaceWithOverlappingPredicate_ThrowsException2()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.RegisterOpenGeneric(typeof(IOpenGenericWithPredicate <>), typeof(OpenGenericWithPredicate1 <>),
                                          Lifestyle.Transient, c => c.ImplementationType.GetGenericArguments().Single() == typeof(int));

            container.RegisterOpenGeneric(typeof(IOpenGenericWithPredicate <>), typeof(OpenGenericWithPredicate2 <>),
                                          Lifestyle.Transient, c => c.ImplementationType.GetGenericArguments().Single().Namespace.StartsWith("System"));

            // Act
            var    result1 = container.GetInstance <IOpenGenericWithPredicate <long> >();
            Action action  = () =>
                             container.GetInstance <IOpenGenericWithPredicate <int> >();

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains <ActivationException>(
                "Multiple observers of the ResolveUnregisteredType event",
                action,
                "GetInstance should fail because the framework should detect that more than one " +
                "implementation of the requested service.");
        }
Esempio n. 5
0
        public void Register_ConstructorResolutionBehaviorThatReturnsBothNullCtorAndNullErrorMessage_ThrowsExpressiveErrorMessage()
        {
            // Arrange
            var container = new Container();

            container.Options.ConstructorResolutionBehavior = new FakeConstructorResolutionBehavior
            {
                ErrorMessage        = null,
                ConstructorToReturn = null
            };

            // Act
            Action action = () => container.Register <RealTimeProvider>();

            // Assert
            AssertThat.ThrowsWithExceptionMessageContains <InvalidOperationException>($@"
                For the container to be able to create {nameof(RealTimeProvider)} it should have a constructor
                that can be called, but according to the customly configured IConstructorResolutionBehavior of
                type {typeof(FakeConstructorResolutionBehavior).ToFriendlyName()}, there is no selectable
                constructor. The {typeof(FakeConstructorResolutionBehavior).ToFriendlyName()}, however, 
                didn't supply a reason why."
                                                                                      .TrimInside(),
                                                                                      action);
        }