Esempio n. 1
0
        public void GetInstance_SingletonThatIteratesArrayInCtorInjectedWithArrayWithTransient_ThrowsMismatchDetected()
        {
            // Arrange
            Type dependencyType = typeof(ILogger[]);

            var container = new Container();

            container.Collection.Append <ILogger, ConsoleLogger>(Lifestyle.Transient);

            container.RegisterSingleton(
                typeof(ILogger),
                typeof(CaptivatingCompositeLogger <>).MakeGenericType(dependencyType));

            // 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>(
                "Resolving services from an injected collection during object construction",
                action);

            AssertThat.ThrowsWithExceptionMessageContains <ActivationException>(
                "lifestyle mismatch has been detected",
                action);
        }
        public void GetInstance_SingletonThatGetsInjectedWithListWithTransients_ThrowsMismatchDetected()
        {
            // Arrange
            var container = ContainerFactory.New();

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

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

            // Assert
            AssertThat.ThrowsWithExceptionMessageDoesNotContain <ActivationException>(
                ResolvingServicesFromAnInjectedCollectionMessage,
                action);

            AssertThat.ThrowsWithExceptionMessageContains <ActivationException>(
                "lifestyle mismatch has been detected",
                action);
        }
        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);
        }