示例#1
0
        public void ShifterResolve_AClassIsRegisteredWithDependedClassThatDoesImplementIt_AnExceptionIsThrown()
        {
            var shifter = new ShifterContainer();

            shifter.AddInstance(new RegisterClassBase().GetType(), new ResolveTestClass());
            shifter.AddInstance(typeof(String), new ResolveTestClass());
        }
示例#2
0
        public void ShifterAddInstance_SameInterfaceAndClassAreAdded_AnExcpetionIsNotThrown()
        {
            Action action = () =>
            {
                var shifter = new ShifterContainer();
                shifter.AddInstance <IResolveTest>(new ImplementsResolveTest());
                shifter.AddInstance(typeof(IResolveTest), new ImplementsResolveTest());
            };

            action.Should().NotThrow <ArgumentException>("because this cannot occur.");
        }
示例#3
0
        public void WhenAllInstancesOfATypeAreResolvedGenericallyItShouldReturnAllInstances()
        {
            // Arrange
            var expectedItemsList = new List <string> {
                "All", "Instances", "Are", "Retrieved"
            };
            var container = new ShifterContainer();

            container
            .AddInstance(typeof(int), 1)
            .AddInstance(typeof(string), "All")
            .AddInstance(typeof(int), 1)
            .AddInstance(typeof(string), "Instances")
            .AddInstance(typeof(int), 1)
            .AddInstance(typeof(string), "Are")
            .AddInstance(typeof(int), 1)
            .AddInstance(typeof(string), "Retrieved");

            // Act
            var resolveAll = container.GetAllInstances <String>();

            // Assert
            resolveAll.Count().Should().Be(4, "because there are 4 instances registered.");
            resolveAll.Should().Contain(expectedItemsList);
        }
示例#4
0
        public void ShifterResolve_ARegisteredClassIsResolved_TheClassIsResolvedToADependedClass()
        {
            var shifter = new ShifterContainer();

            shifter.AddInstance(new RegisterClassBase().GetType(), new RegisterClassDerived());
            var resolvedClass = shifter.Resolve(typeof(RegisterClassBase)) as RegisterClassDerived;

            Assert.IsNotNull(resolvedClass);
            Assert.IsInstanceOfType(resolvedClass, typeof(RegisterClassDerived));
        }
示例#5
0
        public void ShifterResolve_ARegisteredInterfaceIsResolved_TheInterfaceIsResolvedToADependedClass()
        {
            var shifter = new ShifterContainer();

            shifter.AddInstance(typeof(IResolveTest), new DefaultConstructorResolveTest());
            var resolvedClass = shifter.Resolve(typeof(IResolveTest)) as DefaultConstructorResolveTest;

            Assert.IsNotNull(resolvedClass);
            Assert.IsInstanceOfType(resolvedClass, typeof(DefaultConstructorResolveTest));
        }
示例#6
0
        public void ShifterResolve_ARegisteredClassIsResolved_TheClassIsResolved()
        {
            var shifter = new ShifterContainer();

            shifter.AddInstance(new ResolveTestClass());
            var resolvedClass = shifter.Resolve(typeof(ResolveTestClass)) as ResolveTestClass;

            Assert.IsNotNull(resolvedClass);
            Assert.IsInstanceOfType(resolvedClass, typeof(ResolveTestClass));
        }
示例#7
0
        public void ShifterResolve_MethodIsInjected_TheMethodIsResolved()
        {
            const string injectedValue = "Tron";

            var shifter = new ShifterContainer();
            var resolvedClassWithOneParam = (ImplementsResolveTestWithParameters)shifter.AddInstance(injectedValue)
                                            .AddType(typeof(IResolveTest), typeof(ImplementsResolveTestWithParameters))
                                            .Resolve <IResolveTest>();

            Assert.AreEqual(injectedValue, resolvedClassWithOneParam.MethodFieldInjected);
        }
示例#8
0
        public void ShifterContainerResolve_PropertyInjected_ThePropertyIsInjctedWithTheClassProvided()
        {
            const string valueToInject = "hello world";

            var shifter       = new ShifterContainer();
            var resolvedClass = shifter
                                .AddInstance(typeof(string), valueToInject)
                                .AddInstance(typeof(ImplementsResolveTest), new ImplementsResolveTest())
                                .Resolve <ImplementsResolveTest>();

            Assert.AreEqual(valueToInject, resolvedClass.InjectedProperty);
        }
示例#9
0
        public void WhenCallingGetServiceItShouldReturnTheValueRegisteredForTheService()
        {
            // Arrange
            var container = new ShifterContainer();

            container.AddInstance("The string that should be returned.");

            // Act
            var service = container.GetService(typeof(string));

            // Assert
            service.Should().Be("The string that should be returned.", "because this string should be returned.");
        }
示例#10
0
        public void WhenCallingResetItShouldBePossibleToRegisterInstancesAgain()
        {
            // Arrange
            var container = new ShifterContainer();

            // Act
            container.Reset();
            container.AddInstance("string");
            var resolvedValue = container.Resolve <string>();

            // Assert
            resolvedValue.Should().Be("string", "because we added it.");
        }
示例#11
0
        public void WhenCallingGenericGetInstanceItShouldReturnTheInstanceRegisteredForTheCall()
        {
            // Arrange
            var container = new ShifterContainer();

            container.AddInstance("The string that should be returned.");

            // Act
            var instance = container.GetInstance <string>();

            // Assert
            instance.Should().Be("The string that should be returned.", "because this string should be returned.");
        }
示例#12
0
        public void ShifterAddType_AdditionOfTypeWithConstructorWithParameters_TheTypeIsResolved()
        {
            const string injectedValue = "Tron";

            var shifter = new ShifterContainer();
            var resolvedClassWithOneParam = (ImplementsResolveTestWithParameters)shifter.AddInstance(injectedValue)
                                            .AddType(typeof(IResolveTest), typeof(ImplementsResolveTestWithParameters))
                                            .Resolve <IResolveTest>();

            Assert.IsNotNull(resolvedClassWithOneParam);
            Assert.IsInstanceOfType(resolvedClassWithOneParam, typeof(ImplementsResolveTestWithParameters));
            Assert.AreEqual(injectedValue, resolvedClassWithOneParam.Param1);
            Assert.AreEqual(injectedValue, resolvedClassWithOneParam.FieldToInject);

            var resolvedClass = shifter.AddInstance(typeof(ImplementsResolveTestWithParameters), resolvedClassWithOneParam)
                                .AddType <ImplementsResolveTestWith2Parameters, ImplementsResolveTestWith2Parameters>()
                                .Resolve <ImplementsResolveTestWith2Parameters>();

            Assert.IsNotNull(resolvedClass);
            Assert.IsInstanceOfType(resolvedClass, typeof(ImplementsResolveTestWith2Parameters));
            Assert.AreEqual(injectedValue, resolvedClass.Param1);
            Assert.AreSame(resolvedClassWithOneParam, resolvedClass.Param2);
        }
示例#13
0
        public void ShifterAddInstance_AddInstanceReturnsShiftContainer_TheShiftContainerIsReturned()
        {
            var shifter = new ShifterContainer();
            IShifterContainer container = shifter.AddInstance(new object());

            Assert.AreSame(shifter, container);

            container = shifter.AddInstance <IResolveTest>(new ImplementsResolveTest());

            Assert.AreSame(shifter, container);

            container = shifter.AddInstance(typeof(ResolveTestClass), new ResolveTestClass());

            Assert.AreSame(shifter, container);

            container = shifter.AddType(typeof(IComparable), typeof(String));

            Assert.AreSame(shifter, container);

            container = shifter.AddType <IResolveTest2, ImplementsResolveTest2>();

            Assert.AreSame(shifter, container);
        }
示例#14
0
        public void WhenResettingItShouldUnregisterAllInstancesOfThatType()
        {
            // Arrange
            var container = new ShifterContainer();

            container
            .AddInstance(typeof(int), 1234)
            .AddInstance(typeof(string), "Hello")
            .AddInstance(typeof(int), 2)
            .AddInstance(typeof(IShifterContainer), ShifterContainer.Default);

            // Act
            container.Reset();
            var resolvedInts      = container.ResolveAll <int>();
            var resolvedString    = container.ResolveAll <string>();
            var resolvedContainer = container.ResolveAll <IShifterContainer>();

            // Assert
            resolvedInts.Should().BeEmpty("because these ints are unregistered.");
            resolvedString.Should().BeEmpty("because these strings are unregistered.");
            resolvedContainer.Should().BeEmpty("because these containers are unregistered.");
        }
示例#15
0
        public void WhenUnregisteringATypeGenericallyItShouldUnregisterAllInstancesOfThatType()
        {
            // Arrange
            var container = new ShifterContainer();

            container
            .AddInstance(typeof(int), 1234)
            .AddInstance(typeof(string), "Hello")
            .AddInstance(typeof(int), 2)
            .AddInstance(typeof(IShifterContainer), ShifterContainer.Default);

            // Act
            container.Unregister <int>();
            var resolvedInts      = container.ResolveAll <int>();
            var resolvedString    = container.ResolveAll <string>();
            var resolvedContainer = container.ResolveAll <IShifterContainer>();

            // Assert
            resolvedInts.Should().BeEmpty("because these are unregistered.");
            resolvedString.Count().Should().Be(1, "because one string was left.");
            resolvedContainer.Count().Should().Be(1, "because one IShifterContainer was left.");
        }
示例#16
0
        public void ShifterRegister2Params_FirstParamIsNull_AnExceptionIsThrown()
        {
            var shifter = new ShifterContainer();

            shifter.AddInstance(null, new ResolveTestClass());
        }
示例#17
0
        public void ShifterRegister2Params_SecondParamIsNull_AnExceptionIsThrown()
        {
            var shifter = new ShifterContainer();

            shifter.AddInstance(new RegisterClassBase().GetType(), null);
        }