Ejemplo n.º 1
0
        public void TestBindWhenIntoByInstance()
        {
            var instance1 = new MockClassSimple();
            var instance2 = new MockClassVerySimple();

            var container = new InjectionContainer();

            container.Bind <IMockInterface>().To <MockIClassWithAttributes>().WhenIntoInstance(instance1);
            container.Bind <IMockInterface>().To <MockIClassWithoutAttributes>().WhenIntoInstance(instance2);

            container.Inject(instance1);
            container.Inject(instance2);

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.field.GetType());
        }
Ejemplo n.º 2
0
        public void TestBindAsIdentifierWhenIntoByInstance()
        {
            var instance1 = new MockClassVerySimple();
            var instance2 = new MockClassSimple();

            var container = new InjectionContainer();

            container.Bind<IMockInterface>()
                .To<MockIClassWithAttributes>().WhenIntoInstance(instance1);
            container.Bind<IMockInterface>()
                .To<MockIClassWithoutAttributes>().WhenIntoInstance(instance2).As("singleton");
            container.Bind<IMockInterface>()
                .To<MockIClass>().WhenInto<MockClassSimple>().WhenIntoInstance(instance2).As("test");

            container.Inject(instance1);
            container.Inject(instance2);

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.IsNull(instance2.field);
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.property.GetType());
        }
Ejemplo n.º 3
0
        public void TestBindAsIdentifierWhenIntoByInstance()
        {
            var instance1 = new MockClassVerySimple();
            var instance2 = new MockClassSimple();

            var container = new InjectionContainer();

            container.Bind <IMockInterface>()
            .To <MockIClassWithAttributes>().WhenIntoInstance(instance1);
            container.Bind <IMockInterface>()
            .To <MockIClassWithoutAttributes>().WhenIntoInstance(instance2).As("singleton");
            container.Bind <IMockInterface>()
            .To <MockIClass>().WhenInto <MockClassSimple>().WhenIntoInstance(instance2).As("test");

            container.Inject(instance1);
            container.Inject(instance2);

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.IsNull(instance2.field);
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.property.GetType());
        }
Ejemplo n.º 4
0
        public void TestBindWhenComplexConditionField()
        {
            var instance1 = new MockClassVerySimple();
            var instance2 = new MockClassSimple();
            var instance3 = new MockClassSimple();

            var container = new InjectionContainer();

            container.Bind <IMockInterface>().To <MockIClass>().When(context =>
                                                                     context.member.Equals(InjectionMember.Field) &&
                                                                     context.parentInstance.Equals(instance3)
                                                                     );

            container.Inject(instance1);
            container.Inject(instance2);
            container.Inject(instance3);

            Assert.IsNull(instance1.field);
            Assert.IsNull(instance2.field);
            Assert.IsNull(instance2.property);
            Assert.AreEqual(typeof(MockIClass), instance3.field.GetType());
            Assert.IsNull(instance3.property);
        }
Ejemplo n.º 5
0
        public void TestBindWhenComplexCondition()
        {
            var instance1 = new MockClassVerySimple();
            var instance2 = new MockClassSimple();
            var instance3 = new MockClassSimple();

            var container = new InjectionContainer();

            container.Bind<IMockInterface>().To<MockIClass>().When(context =>
                    context.member.Equals(InjectionMember.Field) &&
                    context.parentInstance.Equals(instance3)
                );

            container.Inject(instance1);
            container.Inject(instance2);
            container.Inject(instance3);

            Assert.IsNull(instance1.field);
            Assert.IsNull(instance2.field);
            Assert.IsNull(instance2.property);
            Assert.AreEqual(typeof(MockIClass), instance3.field.GetType());
            Assert.IsNull(instance3.property);
        }
Ejemplo n.º 6
0
        public void TestBindWhenIntoByInstance()
        {
            var instance1 = new MockClassSimple();
            var instance2 = new MockClassVerySimple();

            var container = new InjectionContainer();

            container.Bind<IMockInterface>().To<MockIClassWithAttributes>().WhenIntoInstance(instance1);
            container.Bind<IMockInterface>().To<MockIClassWithoutAttributes>().WhenIntoInstance(instance2);

            container.Inject(instance1);
            container.Inject(instance2);

            Assert.AreEqual(typeof(MockIClassWithAttributes), instance1.field.GetType());
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance2.field.GetType());
        }