예제 #1
0
        public void ShouldChooseConstructorArgumentBasedOnTypeInjectedIntoAndUseInstance()
        {
            var arg = new AlternateConstructorArgument();

            locator
                .Register(Given<ITestInterface>
                              .When<TestEnum>(test => test == TestEnum.Case1)
                              .Then<DependsOnInterface>())
                .Register(Given<ITestInterface>
                              .When<TestEnum>(test => test == TestEnum.Case2)
                              .Then<DependsOnAlternateConstructorImplicitly>())
                .Register(Given<IConstructorArgument>
                              .WhenInjectingInto<DependsOnInterface>()
                              .Then<ConstructorArgument>())
                .Register(Given<IConstructorArgument>
                              .WhenInjectingInto<DependsOnAlternateConstructorImplicitly>()
                              .Then(arg));

            locator.AddContext(TestEnum.Case2);

            var instance = locator.GetInstance<ITestInterface>();

            Assert.IsInstanceOf<DependsOnAlternateConstructorImplicitly>(instance);
            Assert.IsInstanceOf<AlternateConstructorArgument>(((DependsOnAlternateConstructorImplicitly)instance).Argument);
            Assert.AreSame(arg, ((DependsOnAlternateConstructorImplicitly) instance).Argument);
        }
예제 #2
0
        public void ShouldChooseConstructorArgumentBasedOnTypeInjectedIntoAndUseInstance()
        {
            var arg = new AlternateConstructorArgument();

            locator
            .Register(Given <ITestInterface>
                      .When <TestEnum>(test => test == TestEnum.Case1)
                      .Then <DependsOnInterface>())
            .Register(Given <ITestInterface>
                      .When <TestEnum>(test => test == TestEnum.Case2)
                      .Then <DependsOnAlternateConstructorImplicitly>())
            .Register(Given <IConstructorArgument>
                      .WhenInjectingInto <DependsOnInterface>()
                      .Then <ConstructorArgument>())
            .Register(Given <IConstructorArgument>
                      .WhenInjectingInto <DependsOnAlternateConstructorImplicitly>()
                      .Then(arg));

            locator.AddContext(TestEnum.Case2);

            var instance = locator.GetInstance <ITestInterface>();

            Assert.IsInstanceOf <DependsOnAlternateConstructorImplicitly>(instance);
            Assert.IsInstanceOf <AlternateConstructorArgument>(((DependsOnAlternateConstructorImplicitly)instance).Argument);
            Assert.AreSame(arg, ((DependsOnAlternateConstructorImplicitly)instance).Argument);
        }