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)); }
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)); }
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)); }
public void ShouldActivateRegisteredType() { var sut = new NSubContainer().Register <ITestInterface, TestImplSingleCtor>(NSubLifetime.Transient); var result = sut.Resolve <ITestInterface>(); Assert.That(result, Is.Not.Null); }
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)); }
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>()); }
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")); }
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>()); }
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>()); }
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); }
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)); }
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>()); }
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)); }