public void TestUnregisterInstanceWithKey()
        {
            var instanceOriginal1 = new TestClass();
            var instanceOriginal2 = new TestClass();
            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<TestClass>(key1);
            Assert.AreSame(instanceOriginal1, instance1);
            var instance2 = SimpleIoc.Default.GetInstance<TestClass>(key2);
            Assert.AreSame(instanceOriginal2, instance2);

            SimpleIoc.Default.Unregister<TestClass>(key1);

            try
            {
                SimpleIoc.Default.GetInstance<TestClass>(key1);
                Assert.Fail("ActivationException was expected");
            }
            catch (ActivationException)
            {
            }
        }
Ejemplo n.º 2
0
        public void TestContainsInstance()
        {
            SimpleIoc.Default.Reset();
            const string key1 = "My key";
            var instance = new TestClass();
            SimpleIoc.Default.Register(() => instance, key1);
            SimpleIoc.Default.Register<TestClass2>();

            Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass>());
            Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass>(key1));
            Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass2>());
            Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass3>());

            SimpleIoc.Default.GetInstance<TestClass>(key1);

            Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass>());
            Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass>(key1));
            Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass2>());
            Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass3>());

            SimpleIoc.Default.GetInstance<TestClass2>();

            Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass>());
            Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass>(key1));
            Assert.IsTrue(SimpleIoc.Default.ContainsCreated<TestClass2>());
            Assert.IsFalse(SimpleIoc.Default.ContainsCreated<TestClass3>());
        }
        public void TestIsClassRegisteredWithFactory()
        {
            SimpleIoc.Default.Reset();

            var instance = new TestClass();
            Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass>());
            SimpleIoc.Default.Register(() => instance);
            Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass>());

            SimpleIoc.Default.GetInstance<TestClass>();
            Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass>());

            SimpleIoc.Default.Unregister<TestClass>();
            Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass>());
        }
        public void TestUnregisterInstance()
        {
            var instanceOriginal1 = new TestClass();

            SimpleIoc.Default.Reset();
            SimpleIoc.Default.Register(() => instanceOriginal1);

            var instance1 = SimpleIoc.Default.GetInstance<TestClass>();
            Assert.AreSame(instanceOriginal1, instance1);

            SimpleIoc.Default.Unregister(instanceOriginal1);

            try
            {
                SimpleIoc.Default.GetInstance<TestClass>();
                Assert.Fail("ActivationException was expected");
            }
            catch (ActivationException)
            {
            }
        }
        public void TestIsClassRegisteredWithFactoryAndKey()
        {
            SimpleIoc.Default.Reset();
            const string key1 = "My key 1";
            const string key2 = "My key 2";

            var instance = new TestClass();
            Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass>(key1));
            Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass>(key2));

            SimpleIoc.Default.Register(() => instance, key1);
            Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass>(key1));
            Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass>(key2));

            SimpleIoc.Default.GetInstance<TestClass>(key1);
            Assert.IsTrue(SimpleIoc.Default.IsRegistered<TestClass>(key1));

            SimpleIoc.Default.Unregister<TestClass>(key1);
            Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass>(key1));
            Assert.IsFalse(SimpleIoc.Default.IsRegistered<TestClass>(key2));
        }