Beispiel #1
0
        public void RegisterSingle_RegisteringANonConcreteType_ThrowsAnArgumentExceptionWithExpectedMessage()
        {
            // Arrange
            string expectedMessage = typeof(IUserRepository).Name + " is not a concrete type.";

            var container = ContainerFactory.New();

            try
            {
                // Act
                container.RegisterSingle <IUserRepository>();

                Assert.Fail("The abstract type was not expected to be registered successfully.");
            }
            catch (ArgumentException ex)
            {
                AssertThat.IsInstanceOfType(typeof(ArgumentException), ex, "No subtype was expected.");
                AssertThat.StringContains(expectedMessage, ex.Message);
            }
        }
        public void ContainerGetInstance_ResolvingAnInstanceWithDependencies_OnlyCallsTheDelegateForThisRootInstance()
        {
            // Arrange
            var resolvedInstances = new InstanceInitializationDataCollection();

            var container = new Container();

            container.Options.ResolveUnregisteredConcreteTypes = true;

            container.Options.RegisterResolveInterceptor(resolvedInstances.Intercept, Always);

            // Act
            container.GetInstance <Dep1>();

            object actualInstance = resolvedInstances.First().Instance;

            // Assert
            AssertThat.IsInstanceOfType(typeof(Dep1), actualInstance, resolvedInstances.ToString());
            Assert.AreEqual(1, resolvedInstances.Count, resolvedInstances.ToString());
        }
Beispiel #3
0
        public void GetInstance_ServiceRegisteredUsingRegisterInstanceNonGeneric_CallsExpressionBuildingWithConstantExpression()
        {
            // Arrange
            var expressionsBuilding = new List <Expression>();

            var container = ContainerFactory.New();

            container.RegisterInstance(typeof(IUserRepository), new SqlUserRepository());

            container.ExpressionBuilding += (s, e) =>
            {
                expressionsBuilding.Add(e.Expression);
            };

            // Act
            container.GetInstance <IUserRepository>();

            // Assert
            Assert.AreEqual(1, expressionsBuilding.Count);
            AssertThat.IsInstanceOfType(typeof(ConstantExpression), expressionsBuilding.Single());
        }
        public void GetInstance_CalledTwiceForSingletonRegistrationWithTransientDecorator_CallsEventOnceForInstanceTwiceForDecorator()
        {
            // Arrange
            var actualContexts = new List <InstanceInitializationData>();

            var container = new Container();

            container.Register <RealTimeProvider>();

            container.RegisterInitializer(actualContexts.Add, TruePredicate);

            container.RegisterSingle <ICommandHandler <RealCommand>, StubCommandHandler>();
            container.RegisterDecorator(typeof(ICommandHandler <>), typeof(RealCommandHandlerDecorator));

            // Act
            var decorator1 = container.GetInstance <ICommandHandler <RealCommand> >() as RealCommandHandlerDecorator;
            var decorator2 = container.GetInstance <ICommandHandler <RealCommand> >() as RealCommandHandlerDecorator;

            // Assert
            Assert.AreEqual(3, actualContexts.Count, "Three event args were expected.");

            AssertThat.IsInstanceOfType(typeof(StubCommandHandler), actualContexts.First().Instance);

            Assert.AreSame(decorator1.Decorated, actualContexts.First().Instance);

            Assert.AreEqual(
                expected: typeof(RealCommandHandlerDecorator),
                actual: actualContexts.Second().Context.Registration.ImplementationType);

            Assert.AreSame(decorator1, actualContexts.Second().Instance);

            Assert.AreEqual(
                expected: typeof(RealCommandHandlerDecorator),
                actual: actualContexts.Last().Context.Registration.ImplementationType);

            Assert.AreSame(decorator2, actualContexts.Last().Instance);
        }
        public void ExpressionBuilding_OnInstanceWithInitializer_GetsExpressionWhereInitializerIsNotAppliedYet()
        {
            // Arrange
            Expression actualBuildingExpression = null;

            var container = ContainerFactory.New();

            container.Register <IUserRepository, SqlUserRepository>();

            container.RegisterInitializer <SqlUserRepository>(repository => { });

            container.ExpressionBuilding += (sender, e) =>
            {
                Assert.AreEqual(e.KnownImplementationType, typeof(SqlUserRepository), "Test setup fail.");
                actualBuildingExpression = e.Expression;
            };

            // Act
            container.GetInstance <IUserRepository>();

            // Assert
            AssertThat.IsInstanceOfType(typeof(NewExpression), actualBuildingExpression, "The initializer is expected to be applied AFTER the ExpressionBuilding event ran. " +
                                        "This makes it much easier to alter the given expression.");
        }
        public void GetInstance_OnInstanceRegisteredAsSingleton_ExpressionBuiltGetsFiredWithConstantExpression()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.RegisterSingle <IUserRepository, SqlUserRepository>();

            Expression actualExpression = null;

            container.ExpressionBuilt += (sender, e) =>
            {
                if (e.RegisteredServiceType == typeof(IUserRepository))
                {
                    actualExpression = e.Expression;
                }
            };

            // Act
            container.GetInstance <IUserRepository>();

            // Assert
            Assert.IsNotNull(actualExpression);
            AssertThat.IsInstanceOfType(typeof(ConstantExpression), actualExpression);
        }
        public void InjectingEnumerableOfMetadata_ForDecoratedInstances_Succeeds()
        {
            // Arrange
            var container = ContainerFactory.New();

            container.Collection.Append(typeof(IEventHandler<>), typeof(NotifyCustomer), Lifestyle.Singleton);
            container.Collection.Append(typeof(IEventHandler<>), typeof(DetermineNewWarehouseInventory));

            container.RegisterDecorator(typeof(IEventHandler<>), typeof(EventHandlerDecorator<>));

            // Act
            var service = container.GetInstance<EnumerableMetadata<IEventHandler<OrderShipped>>>();

            container.Verify();

            var metadatas = service.Metadatas;
            var notifyDecorator = (EventHandlerDecorator<OrderShipped>)metadatas.First().GetInstance();
            var determineDecorator = (EventHandlerDecorator<OrderShipped>)metadatas.Last().GetInstance();

            // Assert
            Assert.AreEqual(2, metadatas.Count());
            AssertThat.IsInstanceOfType<NotifyCustomer>(notifyDecorator.Decoratee);
            AssertThat.IsInstanceOfType<DetermineNewWarehouseInventory>(determineDecorator.Decoratee);
        }