public void GetAllToConstant()
        {
            TeenyKernel kernel = new TeenyKernel();

            RandomIdClass class4 = new RandomIdClass();

            kernel.Bind <Interface1>().ToConstant(class4);
            kernel.Bind <Interface1>().ToMethod(_ => class4);
            kernel.Bind <Interface1>().ToMethod(_ => new RandomIdClass());

            IEnumerable <Interface1> interfaces = kernel.GetAll <Interface1>();

            Assert.IsTrue(interfaces.Any());
            Assert.IsFalse(interfaces.Any(i => i is null));

            string id1 = interfaces.ElementAt(0).Test();
            string id2 = interfaces.ElementAt(1).Test();
            string id3 = interfaces.ElementAt(2).Test();

            // First two should be same instance
            Assert.AreEqual(id1, id2);

            // Last one is unique
            Assert.AreNotEqual(id1, id3);
        }
示例#2
0
        public void ToConstant()
        {
            TeenyKernel kernel = new TeenyKernel();

            RandomIdClass class4 = new RandomIdClass();

            kernel.Bind <Interface1>().ToConstant(class4);

            Interface1 test1 = kernel.Get <Interface1>();
            Interface1 test2 = kernel.Get <Interface1>();

            Assert.AreEqual(test1.Test(), test2.Test());
        }
示例#3
0
        public void ToMethod()
        {
            TeenyKernel kernel = new TeenyKernel();

            RandomIdClass class4 = new RandomIdClass();

            kernel.Bind <Interface1>().ToMethod(_ => class4);

            Interface1 test1 = kernel.Get <Interface1>();
            Interface1 test2 = kernel.Get <Interface1>();

            Assert.AreEqual(test1.Test(), test2.Test());

            // Rebind with new method
            kernel.Rebind <Interface1>().ToMethod(_ => new RandomIdClass());

            Interface1 test3 = kernel.Get <Interface1>();
            Interface1 test4 = kernel.Get <Interface1>();

            Assert.AreNotEqual(test3.Test(), test4.Test());
        }