コード例 #1
0
        public void ShouldCreateNewContainerInstanceOnCustomize()
        {
            var sut = new NSubContainer();

            var sutFork = sut.Customize();

            Assert.That(sutFork, Is.Not.SameAs(sut));
        }
コード例 #2
0
        public void ShouldReturnFromParentContainerIfNoForkCustomizations()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

            var sutFork = sut.Customize().Customize().Customize();

            var result = sutFork.Resolve <ITestInterface>();

            Assert.That(result, Is.AssignableTo <TestImplSingleCtor>());
        }
コード例 #3
0
        public void ShouldUseRegistrationFromForkContainerIfRequestComesFromParentContainerRegistration()
        {
            var sut = new NSubContainer();

            sut.Register <ClassWithDependency, ClassWithDependency>(NSubLifetime.Transient);
            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);
            var sutFork = sut.Customize().Register <ITestInterface, TestImplSingleCtor2>(NSubLifetime.Transient);

            var sutForkResult = sutFork.Resolve <ClassWithDependency>();

            Assert.That(sutForkResult.Dep, Is.AssignableTo <TestImplSingleCtor2>());
        }
コード例 #4
0
        public void ShouldNotModifyOriginalContainerOnCustomize()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

            var sutFork = sut.Customize().Register <ITestInterface, TestImplSingleCtor2>(NSubLifetime.Transient);

            var sutResult     = sut.Resolve <ITestInterface>();
            var sutForkResult = sutFork.Resolve <ITestInterface>();

            Assert.That(sutResult, Is.AssignableTo <TestImplSingleCtor>());
            Assert.That(sutForkResult, Is.AssignableTo <TestImplSingleCtor2>());
        }
コード例 #5
0
        public void ShouldPinDecoratedRegistrationAtRegistrationTime()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);
            var sutFork = sut.Customize().Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));

            // Override registration. Very rare case
            sut.Register <ITestInterface, TestImplSingleCtor2>(NSubLifetime.Transient);
            var result = sutFork.Resolve <ITestInterface>();

            Assert.That(result, Is.TypeOf <TestImplDecorator>());
            Assert.That(((TestImplDecorator)result).Inner, Is.TypeOf <TestImplSingleCtor>());
        }
コード例 #6
0
        public void ShouldDecorateTheExistingRegistration()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.PerScope);

            var sutFork = sut
                          .Customize()
                          .Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var result = sutFork.Resolve <ITestInterface>();

            Assert.That(result, Is.TypeOf <TestImplDecorator>());
            Assert.That(((TestImplDecorator)result).Inner, Is.TypeOf <TestImplSingleCtor>());
        }
コード例 #7
0
        public void ShouldBePossibleToCreateNestedDecorators()
        {
            var sut = new NSubContainer();

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.PerScope);

            var sutFork = sut
                          .Customize()
                          .Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var sutForkFork = sutFork
                              .Customize()
                              .Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var result = sutForkFork.Resolve <ITestInterface>();

            var mostInner = ((result as TestImplDecorator)?.Inner as TestImplDecorator)?.Inner;

            Assert.That(mostInner, Is.TypeOf <TestImplSingleCtor>());
        }