Example #1
0
        public void ShouldReturnSameInstanceForNewRequestForSingletonLifetime()
        {
            var sut = new NSubContainer();

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

            var result1 = sut.Resolve <ITestInterface>();
            var result2 = sut.Resolve <ITestInterface>();

            Assert.That(result2, Is.SameAs(result1));
        }
Example #2
0
        public void ShouldReturnNewInstanceForEachRequestForTransientLifetime()
        {
            var sut = new NSubContainer();

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

            var result1 = sut.Resolve <ITestInterface>();
            var result2 = sut.Resolve <ITestInterface>();

            Assert.That(result2, Is.Not.SameAs(result1));
        }
Example #3
0
        public void ShouldUseSameLifetimeForDecorator_SingletonCase()
        {
            var sut = new NSubContainer();

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

            sut.Decorate <ITestInterface>((impl, r) => new TestImplDecorator(impl));
            var result1 = sut.Resolve <ITestInterface>();
            var result2 = sut.Resolve <ITestInterface>();

            Assert.That(result1, Is.SameAs(result2));
            Assert.That(((TestImplDecorator)result1).Inner, Is.SameAs(((TestImplDecorator)result2).Inner));
        }
Example #4
0
        public void ShouldActivateRegisteredType()
        {
            var sut = new NSubContainer().Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient);

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

            Assert.That(result, Is.Not.Null);
        }
Example #5
0
        public void ShouldFailWithMeaningfulExceptionIfUnableToResolveType()
        {
            var sut = new NSubContainer();

            var ex = Assert.Throws <InvalidOperationException>(() => sut.Resolve <ITestInterface>());

            Assert.That(ex.Message, Contains.Substring("not registered"));
            Assert.That(ex.Message, Contains.Substring(typeof(ITestInterface).FullName));
        }
Example #6
0
        public void ShouldResolveDependencies()
        {
            var sut = new NSubContainer();

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

            var result = sut.Resolve <ClassWithDependency>();

            Assert.That(result.Dep, Is.AssignableTo <TestImplSingleCtor>());
        }
Example #7
0
        public void ShouldAllowToRegisterTypeWithMultipleCtorsUsingFactoryMethod()
        {
            var sut = new NSubContainer();

            sut.Register(_ => new TestImplMultipleCtors("42"), NSubLifetime.Transient);

            var result = sut.Resolve <TestImplMultipleCtors>();

            Assert.That(result, Is.Not.Null);
            Assert.That(result.Value, Is.EqualTo("42"));
        }
Example #8
0
        public void ShouldUseNewRegistrationOnRepeatedRegister()
        {
            var sut = new NSubContainer();

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

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

            Assert.That(result, Is.AssignableTo <TestImplSingleCtor2>());
        }
Example #9
0
        public void ShouldDecorateWhenRegisteredOnSameContainer()
        {
            var sut = new NSubContainer();

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

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

            Assert.That(result, Is.TypeOf <TestImplDecorator>());
        }
Example #10
0
        public void ShouldAllowToResolveDependencyInFactoryMethod()
        {
            var sut = new NSubContainer();

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

            sut.Register <ClassWithDependency>(r => new ClassWithDependency(r.Resolve <ITestInterface>()), NSubLifetime.Transient);

            var result = sut.Resolve <ClassWithDependency>();

            Assert.That(result, Is.Not.Null);
        }
Example #11
0
        public void ShouldReturnNewInstanceForSameRequestForTransientLifetime()
        {
            var sut = new NSubContainer();

            sut.Register <ClassWithMultipleDependencies, ClassWithMultipleDependencies>(NSubLifetime.Transient);
            sut.Register <ClassWithDependency, ClassWithDependency>(NSubLifetime.Transient);

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

            var result = sut.Resolve <ClassWithMultipleDependencies>();

            Assert.That(result.TestInterfaceDep, Is.Not.SameAs(result.ClassWithDependencyDep.Dep));
        }
Example #12
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>());
        }
Example #13
0
        public void ShouldReturnSameInstanceWhenResolvingDependencyInFactoryMethodForPerScopeLifetime()
        {
            var sut = new NSubContainer();

            sut.Register <ClassWithDependency, ClassWithDependency>(NSubLifetime.Transient);

            sut.Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.PerScope);
            sut.Register <ClassWithMultipleDependencies>(
                r => new ClassWithMultipleDependencies(r.Resolve <ITestInterface>(), r.Resolve <ClassWithDependency>()),
                NSubLifetime.Transient);

            var result = sut.Resolve <ClassWithMultipleDependencies>();

            Assert.That(result.TestInterfaceDep, Is.SameAs(result.ClassWithDependencyDep.Dep));
        }