public void TestUnregisterInstanceWithKey() { var instanceOriginal1 = new TestClass1(); var instanceOriginal2 = new TestClass1(); const string key1 = "My key 1"; const string key2 = "My key 2"; SimpleIoc.Default.Reset(); SimpleIoc.Default.Register(() => instanceOriginal1, key1); SimpleIoc.Default.Register(() => instanceOriginal2, key2); var instance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1); Assert.AreSame(instanceOriginal1, instance1); var instance2 = SimpleIoc.Default.GetInstance<TestClass1>(key2); Assert.AreSame(instanceOriginal2, instance2); SimpleIoc.Default.Unregister<TestClass1>(key1); try { SimpleIoc.Default.GetInstance<TestClass1>(key1); Assert.Fail("ActivationException was expected"); } catch (ActivationException) { } }
public void TestContainsInstance() { SimpleIoc.Default.Reset(); const string key1 = "My key"; var instance = new TestClass1(); SimpleIoc.Default.Register(() => instance, key1); SimpleIoc.Default.Register<TestClass2>(); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass1>()); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass1>(key1)); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass2>()); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass3>()); SimpleIoc.Default.GetInstance<TestClass1>(key1); Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass1>()); Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass1>(key1)); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass2>()); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass3>()); SimpleIoc.Default.GetInstance<TestClass2>(); Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass1>()); Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass1>(key1)); Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass2>()); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass3>()); }
public void TestGetInstanceWithInterface() { SimpleIoc.Default.Reset(); var instanceOriginal = new TestClass1(); SimpleIoc.Default.Register<ITestClass>(() => instanceOriginal); var instance = SimpleIoc.Default.GetInstance(typeof (ITestClass)); Assert.IsNotNull(instance); Assert.IsInstanceOfType(instance, typeof (TestClass1)); Assert.AreSame(instanceOriginal, instance); }
public void TestIsClassRegisteredWithFactory() { SimpleIoc.Default.Reset(); var instance = new TestClass1(); Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass1>()); SimpleIoc.Default.Register(() => instance); Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass1>()); SimpleIoc.Default.GetInstance<TestClass1>(); Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass1>()); SimpleIoc.Default.Unregister<TestClass1>(); Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass1>()); }
public void TestAddingFactoryAndKeyForClassRegisteredWithDefault() { SimpleIoc.Default.Reset(); SimpleIoc.Default.Register<TestClass1>(); const string key1 = "key1"; var instance1 = new TestClass1(); SimpleIoc.Default.Register(() => instance1, key1); var defaultInstance = SimpleIoc.Default.GetInstance<TestClass1>(); var foundInstance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1); Assert.AreSame(instance1, foundInstance1); Assert.AreNotSame(defaultInstance, foundInstance1); }
public void TestBuildInstanceWithMultipleConstructorsNotMarkedWithAttribute() { var property = new TestClass1(); SimpleIoc.Default.Reset(); SimpleIoc.Default.Register(() => new TestClass6(property)); var instance1 = new TestClass6(); Assert.IsNotNull(instance1); Assert.IsNull(instance1.MyProperty); var instance2 = SimpleIoc.Default.GetInstance<TestClass6>(); Assert.IsNotNull(instance2); Assert.IsNotNull(instance2.MyProperty); Assert.AreSame(property, instance2.MyProperty); }
public void TestBuildWithMultipleConstructorsNotMarkedWithAttribute() { var property = new TestClass1(); SimpleIoc.Default.Reset(); SimpleIoc.Default.Register<ITestClass>(() => property); try { SimpleIoc.Default.Register<TestClass6>(); Assert.Fail("ActivationException was expected"); } catch (ActivationException ex) { Debug.WriteLine(ex.Message); } }
public void TestBuildWithMultipleConstructors() { var property = new TestClass1(); SimpleIoc.Default.Reset(); SimpleIoc.Default.Register<ITestClass>(() => property); SimpleIoc.Default.Register<TestClass5>(); var instance1 = new TestClass5(); Assert.IsNotNull(instance1); Assert.IsNull(instance1.MyProperty); var instance2 = SimpleIoc.Default.GetInstance<TestClass5>(); Assert.IsNotNull(instance2); Assert.IsNotNull(instance2.MyProperty); Assert.AreSame(property, instance2.MyProperty); }
public void TestAddingFactoryAndKeyForClassRegisteredWithFactoryAndDifferentKey() { SimpleIoc.Default.Reset(); const string key1 = "key1"; var instance1 = new TestClass1(); SimpleIoc.Default.Register(() => instance1, key1); const string key2 = "key2"; var instance2 = new TestClass1(); SimpleIoc.Default.Register(() => instance2, key2); var foundInstance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1); var foundInstance2 = SimpleIoc.Default.GetInstance<TestClass1>(key2); Assert.AreSame(instance1, foundInstance1); Assert.AreSame(instance2, foundInstance2); Assert.AreNotSame(foundInstance1, foundInstance2); }
public void TestConstructWithProperty() { var property = new TestClass1(); SimpleIoc.Default.Reset(); SimpleIoc.Default.Register( () => new TestClass6 { MyProperty = property }); var instance1 = new TestClass6(); Assert.IsNotNull(instance1); Assert.IsNull(instance1.MyProperty); var instance2 = SimpleIoc.Default.GetInstance<TestClass6>(); Assert.IsNotNull(instance2); Assert.IsNotNull(instance2.MyProperty); Assert.AreSame(property, instance2.MyProperty); }
public void TestIsClassRegisteredWithFactoryAndKey() { SimpleIoc.Default.Reset(); const string key1 = "My key 1"; const string key2 = "My key 2"; var instance = new TestClass1(); Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass1>(key1)); Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass1>(key2)); SimpleIoc.Default.Register(() => instance, key1); Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass1>(key1)); Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass1>(key2)); SimpleIoc.Default.GetInstance<TestClass1>(key1); Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass1>(key1)); SimpleIoc.Default.Unregister<TestClass1>(key1); Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass1>(key1)); Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass1>(key2)); }
public void TestUnregisterInstance() { var instanceOriginal1 = new TestClass1(); SimpleIoc.Default.Reset(); SimpleIoc.Default.Register(() => instanceOriginal1); var instance1 = SimpleIoc.Default.GetInstance<TestClass1>(); Assert.AreSame(instanceOriginal1, instance1); SimpleIoc.Default.Unregister(instanceOriginal1); try { var instance2 = SimpleIoc.Default.GetInstance<TestClass1>(); Assert.AreSame(instance1, instance2); } catch (ActivationException) { Assert.Fail("ActivationException was thrown"); } }
public void TestAddingFactoryAndKeyForClassRegisteredWithFactoryAndSameKey() { SimpleIoc.Default.Reset(); const string key1 = "key1"; var instance1 = new TestClass1(); SimpleIoc.Default.Register(() => instance1, key1); var instance2 = new TestClass1(); try { SimpleIoc.Default.Register(() => instance2, key1); Assert.Fail("InvalidOperationException was expected"); } catch (InvalidOperationException) { } }
public void TestGetAllInstances2() { SimpleIoc.Default.Reset(); var instance = new TestClass1(); SimpleIoc.Default.Register(() => instance); var instances = SimpleIoc.Default.GetAllInstances<TestClass1>(); Assert.AreEqual(1, instances.Count()); SimpleIoc.Default.GetInstance<TestClass1>("key1"); instances = SimpleIoc.Default.GetAllInstances<TestClass1>(); Assert.AreEqual(2, instances.Count()); }
public void TestGetAllInstancesOfClassWithCreation() { SimpleIoc.Default.Reset(); TestClass1.Reset(); var instance0 = new TestClass1(); SimpleIoc.Default.Register(() => instance0, true); const string key1 = "key1"; const string key2 = "key2"; const string key3 = "key3"; var instance1 = new TestClass1(); var instance2 = new TestClass1(); var instance3 = new TestClass1(); SimpleIoc.Default.Register(() => instance1, key1, true); SimpleIoc.Default.Register(() => instance2, key2, true); SimpleIoc.Default.Register(() => instance3, key3); Assert.AreEqual(4, TestClass1.InstancesCount); var instances = SimpleIoc.Default.GetAllCreatedInstances<TestClass1>(); Assert.AreEqual(3, instances.Count()); instances = SimpleIoc.Default.GetAllCreatedInstances<TestClass1>(); SimpleIoc.Default.GetInstance<TestClass1>(key3); Assert.AreEqual(4, instances.Count()); var list = instances.ToList(); foreach (var instance in instances) { if (instance == instance0 || instance == instance1 || instance == instance2 || instance == instance3) { list.Remove(instance); } else { Assert.Fail(); } } Assert.AreEqual(0, list.Count); }
public void TestUnregisterFactoryInstance() { SimpleIoc.Default.Reset(); var instance0 = new TestClass1(); SimpleIoc.Default.Register(() => instance0); Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass1>()); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass1>()); var instance1 = SimpleIoc.Default.GetInstance<TestClass1>(); Assert.AreSame(instance0, instance1); Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass1>()); Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass1>()); var instance2 = SimpleIoc.Default.GetInstance<TestClass1>(); Assert.AreSame(instance0, instance2); SimpleIoc.Default.Unregister(instance0); Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass1>()); Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass1>()); var instance3 = SimpleIoc.Default.GetInstance<TestClass1>(); Assert.AreSame(instance0, instance3); }
public void TestGetAllInstancesWithInstanceGeneric() { const string key1 = "MyKey1"; const string key2 = "MyKey2"; const string key3 = "MyKey3"; const string key4 = "MyKey4"; const string key5 = "MyKey5"; const string key6 = "MyKey6"; var instanceOriginal1 = new TestClass1(); var instanceOriginal2 = new TestClass1(); var instanceOriginal3 = new TestClass1(); var instanceOriginal4 = new TestClass1(); var instanceOriginal5 = new TestClass4(); var instanceOriginal6 = new TestClass4(); SimpleIoc.Default.Reset(); SimpleIoc.Default.Register(() => instanceOriginal1, key1); SimpleIoc.Default.Register(() => instanceOriginal2, key2); SimpleIoc.Default.Register(() => instanceOriginal3, key3); SimpleIoc.Default.Register(() => instanceOriginal4, key4); SimpleIoc.Default.Register(() => instanceOriginal5, key5); SimpleIoc.Default.Register(() => instanceOriginal6, key6); var instance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1); var instance2 = SimpleIoc.Default.GetInstance<TestClass1>(key2); var instance3 = SimpleIoc.Default.GetInstance<TestClass1>(key3); var instance4 = SimpleIoc.Default.GetInstance<TestClass1>(key4); var instance5 = SimpleIoc.Default.GetInstance<TestClass4>(key5); var instance6 = SimpleIoc.Default.GetInstance<TestClass4>(key6); Assert.IsNotNull(instance1); Assert.IsNotNull(instance2); Assert.IsNotNull(instance3); Assert.IsNotNull(instance4); Assert.IsNotNull(instance5); Assert.IsNotNull(instance6); var allInstances = SimpleIoc.Default.GetAllInstances<TestClass1>(); Assert.AreEqual(4, allInstances.Count()); foreach (var instance in allInstances) { Assert.IsNotNull(instance); if (instance.Equals(instance1)) { instance1 = null; } if (instance.Equals(instance2)) { instance2 = null; } if (instance.Equals(instance3)) { instance3 = null; } if (instance.Equals(instance4)) { instance4 = null; } if (instance.Equals(instance5)) { instance5 = null; } if (instance.Equals(instance6)) { instance6 = null; } } Assert.IsNull(instance1); Assert.IsNull(instance2); Assert.IsNull(instance3); Assert.IsNull(instance4); Assert.IsNotNull(instance5); Assert.IsNotNull(instance6); }
public void TestGetInstancesWithInstanceGeneric() { const string key1 = "MyKey1"; const string key2 = "MyKey2"; var instanceOriginal1 = new TestClass1(); var instanceOriginal2 = new TestClass1(); SimpleIoc.Default.Reset(); SimpleIoc.Default.Register<ITestClass>(() => instanceOriginal1, key1); SimpleIoc.Default.Register<ITestClass>(() => instanceOriginal2, key2); var instance1 = SimpleIoc.Default.GetInstance<ITestClass>(key1); var instance2 = SimpleIoc.Default.GetInstance<ITestClass>(key1); var instance3 = SimpleIoc.Default.GetInstance<ITestClass>(key2); Assert.IsNotNull(instance1); Assert.IsNotNull(instance2); Assert.IsNotNull(instance3); Assert.IsInstanceOfType(instance1, typeof (TestClass1)); Assert.IsInstanceOfType(instance2, typeof (TestClass1)); Assert.IsInstanceOfType(instance3, typeof (TestClass1)); Assert.AreSame(instance1, instance2); Assert.AreNotSame(instance1, instance3); }
public void TestReset() { SimpleIoc.Default.Reset(); var instanceOriginal = new TestClass1(); SimpleIoc.Default.Register<ITestClass>(() => instanceOriginal); var instance = SimpleIoc.Default.GetInstance<ITestClass>(); Assert.IsNotNull(instance); SimpleIoc.Default.Reset(); try { SimpleIoc.Default.GetInstance<ITestClass>(); Assert.Fail("ActivationException was expected"); } catch (ActivationException) { } }
public void TestGetAllInstancesGeneric1() { SimpleIoc.Default.Reset(); const string key1 = "key1"; var instance1 = new TestClass1(); SimpleIoc.Default.Register(() => instance1, key1); SimpleIoc.Default.Register<TestClass1>(); var instances = SimpleIoc.Default.GetAllInstances<TestClass1>().ToList(); Assert.AreEqual(2, instances.Count); var getInstance1 = SimpleIoc.Default.GetInstance<TestClass1>(key1); Assert.AreSame(instance1, getInstance1); Assert.IsTrue(instances.Contains(instance1)); instances.Remove(instance1); Assert.AreEqual(1, instances.Count); var getInstance2 = SimpleIoc.Default.GetInstance<TestClass1>(); Assert.AreSame(instances[0], getInstance2); SimpleIoc.Default.GetInstance<TestClass1>("key2"); instances = SimpleIoc.Default.GetAllInstances<TestClass1>().ToList(); Assert.AreEqual(3, instances.Count); }
public void TestGettingDefaultInstanceAfterRegisteringFactoryAndKey() { SimpleIoc.Default.Reset(); const string key1 = "key1"; var instance = new TestClass1(); SimpleIoc.Default.Register(() => instance, key1); try { SimpleIoc.Default.GetInstance<TestClass1>(); Assert.Fail("ActivationException was expected"); } catch (ActivationException) { } }