public void Resolve_SetTypeImplementationWithGenericMethod_ShouldCreateNewInstance()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);

            objectInfo.As <ServiceStub>();

            // Act
            var serviceStub = objectInfo.Resolve();

            // Assert
            Assert.IsInstanceOf(typeof(ServiceStub), serviceStub);
        }
        public void Resolve_SetFactoryWithArguments_ShouldCreateNewInstanceEachTime()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);

            objectInfo.As((arguments) => new ServiceStub());

            // Act
            var serviceStub1 = objectInfo.Resolve();
            var serviceStub2 = objectInfo.Resolve();

            // Assert
            Assert.AreNotSame(serviceStub1, serviceStub2);
        }
        public void Resolve_SetFactoryWithArguments_ShouldCreateNewInstance()
        {
            // Arrange
            var objectInfo  = new ContainerInstance(this.registeredType, this.container);
            var serviceStub = new ServiceStub();

            objectInfo.As((arguments) => serviceStub);

            // Act
            var result = objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub, result);
        }
        public void Resolve_SetTypeImplementationWithGenericMethod_ShouldCreateNewInstanceEachTime()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);

            objectInfo.As <ServiceStub>();

            // Act
            var serviceStub1 = objectInfo.Resolve();
            var serviceStub2 = objectInfo.Resolve();

            // Assert
            Assert.AreNotSame(serviceStub1, serviceStub2);
        }
        public void Resolve_WithArguments_ShouldUseArguments()
        {
            // Arrange
            var          objectInfo = new ContainerInstance(this.registeredType, this.container);
            const string A          = "Test";
            const int    B          = 10;

            objectInfo.As(typeof(ServiceWithConstructorStub));

            // Act
            var result = (ServiceWithConstructorStub)objectInfo.Resolve(new object[] { A, B });

            // Assert
            Assert.AreEqual(A, result.A);
            Assert.AreEqual(B, result.B);
        }
        public void As_SetInterfaceAsTypeImplementation_ThrowsException()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);

            // Act
            TestDelegate testDelegate = () => objectInfo.As(typeof(IServiceStubParent1));

            // Assert
            Assert.Throws<ArgumentException>(
                testDelegate,
                string.Format(
                    CultureInfo.CurrentCulture,
                    InversionOfControlResources.ErrMsg_CannotSetInterfaceAsImplementation,
                    typeof(IServiceStubParent1)));
        }
        public void As_SetInterfaceAsTypeImplementation_ThrowsException()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);

            // Act
            TestDelegate testDelegate = () => objectInfo.As(typeof(IServiceStubParent1));

            // Assert
            Assert.Throws <ArgumentException>(
                testDelegate,
                string.Format(
                    CultureInfo.CurrentCulture,
                    InversionOfControlResources.ErrMsg_CannotSetInterfaceAsImplementation,
                    typeof(IServiceStubParent1)));
        }
        public void Resolve_TypeWithPrivateMethodInjection_ShouldInjectMethod()
        {
            // Arrange
            var serviceStub1 = new ServiceStub();

            this.registrationContext.Register <IServiceStub1>().AsSingleton(serviceStub1);

            var objectInfo = new ContainerInstance(typeof(ServiceWithPrivateMethodInjection), this.container);

            objectInfo.As(typeof(ServiceWithPrivateMethodInjection));

            // Act
            var result = (ServiceWithPrivateMethodInjection)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub1, result.Child);
        }
        public void Resolve_ResolveTypeWithDependency_ShouldAskContainerToResolveArgumentsForCtor()
        {
            // Arrange
            var serviceStub = new ServiceStub();

            this.registrationContext.Register <IServiceStub1>().AsSingleton(serviceStub);

            var objectInfo = new ContainerInstance(typeof(ServiceWithDependencyStub), this.container);

            objectInfo.As(typeof(ServiceWithDependencyStub));

            // Act
            var result = (ServiceWithDependencyStub)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub, result.Child);
        }
        public void As_SetTypeImplementationWithWrongType_ThrowsException()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);

            // Act
            TestDelegate testDelegate = () => objectInfo.As(typeof(UTF8Encoding));

            // Assert
            Assert.Throws <ArgumentException>(
                testDelegate,
                string.Format(
                    CultureInfo.CurrentCulture,
                    InversionOfControlResources.ErrMsg_CannotSetTypeAsImplementation,
                    typeof(UTF8Encoding),
                    this.registeredType));
        }
        public void As_SetTypeImplementationWithWrongTypeForOneOfTwoServices_ThrowsException()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);
            objectInfo.And<IServiceProvider>();

            // Act
            TestDelegate testDelegate = () => objectInfo.As(typeof(ServiceStub));

            // Assert
            Assert.Throws<ArgumentException>(
                testDelegate,
                string.Format(
                    CultureInfo.CurrentCulture,
                    InversionOfControlResources.ErrMsg_CannotSetTypeAsImplementation,
                    typeof(ServiceStub),
                    typeof(IServiceProvider)));
        }
        public void As_SetTypeImplementationWithWrongTypeForOneOfTwoServices_ThrowsException()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);

            objectInfo.And <IServiceProvider>();

            // Act
            TestDelegate testDelegate = () => objectInfo.As(typeof(ServiceStub));

            // Assert
            Assert.Throws <ArgumentException>(
                testDelegate,
                string.Format(
                    CultureInfo.CurrentCulture,
                    InversionOfControlResources.ErrMsg_CannotSetTypeAsImplementation,
                    typeof(ServiceStub),
                    typeof(IServiceProvider)));
        }
        public void InjectionRule_TwoTypesRegistered_ResolvedWithTypeFromRule()
        {
            // Arrange
            var serviceStub1 = new ServiceStub();
            var serviceStub2 = new ServiceStub();

            this.registrationContext.Register <IServiceStub1>().AsSingleton(serviceStub1);
            this.registrationContext.Register <IServiceStub2>().AsSingleton(serviceStub2);

            var objectInfo = new ContainerInstance(typeof(ServiceWithStubBaseInjection), this.container);

            objectInfo.As(typeof(ServiceWithStubBaseInjection));

            // Act
            objectInfo.InjectionRule(typeof(IServiceStubBase), typeof(IServiceStub2));
            var stub = (ServiceWithStubBaseInjection)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub2, stub.Child);
        }
        public void Resolve_WithArguments_ShouldUseArguments()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);
            const string A = "Test";
            const int B = 10;
            objectInfo.As(typeof(ServiceWithConstructorStub));

            // Act
            var result = (ServiceWithConstructorStub)objectInfo.Resolve(new object[] { A, B });

            // Assert
            Assert.AreEqual(A, result.A);
            Assert.AreEqual(B, result.B);
        }
        public void Resolve_TypeWithPrivateMethodInjection_ShouldInjectMethod()
        {
            // Arrange
            var serviceStub1 = new ServiceStub();
            this.registrationContext.Register<IServiceStub1>().AsSingleton(serviceStub1);

            var objectInfo = new ContainerInstance(typeof(ServiceWithPrivateMethodInjection), this.container);
            objectInfo.As(typeof(ServiceWithPrivateMethodInjection));

            // Act
            var result = (ServiceWithPrivateMethodInjection)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub1, result.Child);
        }
        public void Resolve_SetTypeImplementation_ShouldCreateNewInstanceEachTime()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);
            objectInfo.As(typeof(ServiceStub));

            // Act
            var serviceStub1 = objectInfo.Resolve();
            var serviceStub2 = objectInfo.Resolve();

            // Assert
            Assert.AreNotSame(serviceStub1, serviceStub2);
        }
        public void Resolve_SetTypeImplementation_ShouldCreateNewInstance()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);
            objectInfo.As(typeof(ServiceStub));

            // Act
            var serviceStub = objectInfo.Resolve();

            // Assert
            Assert.IsInstanceOf(typeof(ServiceStub), serviceStub);
        }
        public void Resolve_SetFactory_ShouldCreateNewInstanceEachTime()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);
            objectInfo.As(() => new ServiceStub());

            // Act
            var serviceStub1 = objectInfo.Resolve();
            var serviceStub2 = objectInfo.Resolve();

            // Assert
            Assert.AreNotSame(serviceStub1, serviceStub2);
        }
        public void Resolve_SetFactory_ShouldCreateNewInstance()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);
            var serviceStub = new ServiceStub();
            objectInfo.As(() => serviceStub);

            // Act
            var result = objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub, result);
        }
        public void Resolve_ResolveTypeWithDependency_ShouldAskContainerToResolveArgumentsForCtor()
        {
            // Arrange
            var serviceStub = new ServiceStub();
            this.registrationContext.Register<IServiceStub1>().AsSingleton(serviceStub);

            var objectInfo = new ContainerInstance(typeof(ServiceWithDependencyStub), this.container);
            objectInfo.As(typeof(ServiceWithDependencyStub));

            // Act
            var result = (ServiceWithDependencyStub)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub, result.Child);
        }
        public void As_SetTypeImplementationWithWrongType_ThrowsException()
        {
            // Arrange
            var objectInfo = new ContainerInstance(this.registeredType, this.container);

            // Act
            TestDelegate testDelegate = () => objectInfo.As(typeof(UTF8Encoding));

            // Assert
            Assert.Throws<ArgumentException>(
                testDelegate,
                string.Format(
                    CultureInfo.CurrentCulture,
                    InversionOfControlResources.ErrMsg_CannotSetTypeAsImplementation,
                    typeof(UTF8Encoding),
                    this.registeredType));
        }
        public void InjectionRule_TwoTypesRegistered_ResolvedWithTypeFromRule()
        {
            // Arrange
            var serviceStub1 = new ServiceStub();
            var serviceStub2 = new ServiceStub();
            this.registrationContext.Register<IServiceStub1>().AsSingleton(serviceStub1);
            this.registrationContext.Register<IServiceStub2>().AsSingleton(serviceStub2);

            var objectInfo = new ContainerInstance(typeof(ServiceWithStubBaseInjection), this.container);
            objectInfo.As(typeof(ServiceWithStubBaseInjection));

            // Act
            objectInfo.InjectionRule(typeof(IServiceStubBase), typeof(IServiceStub2));
            var stub = (ServiceWithStubBaseInjection)objectInfo.Resolve();

            // Assert
            Assert.AreEqual(serviceStub2, stub.Child);
        }