public void Resolve_ThrowsTypeNotRegisteredExceptionTest() { IContainer container = new IoC.Container(); container.Register <ITestClass2, TestClass2>(); // leave ITestClass3 out for test container.Register <ITestClass4, TestClass4>(); container.Register <ITestClass, TestClass>(); Exception actual = Assert.Throws <TypeNotRegisteredException>( () => container.Resolve(typeof(ITestClass))); Assert.NotNull(actual); Assert.Equal("Type 'IoC.Test.ContainerTests+ITestClass3' not registered with Container instance.", actual.Message); }
public void Resolve_SucceedsTest() { IContainer container = new IoC.Container(); container.Register <ITestClass2, TestClass2>(LifeCycleType.Singleton); container.Register <ITestClass3, TestClass3>(); container.Register <ITestClass4, TestClass4>(); container.Register <ITestClass, TestClass>(LifeCycleType.Transient); var actual = container.Resolve(typeof(ITestClass)) as ITestClass; Assert.NotNull(actual); Assert.NotNull(actual.TestClassProp2); Assert.NotNull(actual.TestClassProp3); Assert.NotNull(actual.TestClassProp3.TestClassProp4); Assert.Equal("TestClass2", actual.GetMyString()); }
public void Register_LifeCycleTypeTransientSucceedsTest() { IContainer container = new IoC.Container(); container.Register <ITestClass4, TestClass4>(); container.Register <ITestClass2, TestClass2>(LifeCycleType.Transient); container.Register <ITestClass3, TestClass3>(LifeCycleType.Transient); container.Register <ITestClass, TestClass>(LifeCycleType.Transient); var actual1 = container.Resolve(typeof(ITestClass)) as ITestClass; var actual2 = container.Resolve(typeof(ITestClass)) as ITestClass; Assert.NotNull(actual1); Assert.NotNull(actual2); Assert.NotEqual(actual1, actual2); Assert.NotEqual(actual1.TestClassProp2, actual2.TestClassProp2); Assert.NotEqual(actual1.TestClassProp3, actual2.TestClassProp3); }
public void UnRegister_Succeeds() { IContainer container = new IoC.Container(); container.Register <ITestClass4, TestClass4>(); var actual = container.Resolve(typeof(ITestClass4)) as ITestClass4; Assert.NotNull(actual); container.UnRegister <ITestClass4>(); Exception ex = Assert.Throws <TypeNotRegisteredException>(() => container.Resolve(typeof(ITestClass4))); Assert.NotNull(ex); Assert.Equal("Type 'IoC.Test.ContainerTests+ITestClass4' not registered with Container instance.", ex.Message); }