Exemplo n.º 1
0
        public void TestBeforeInject()
        {
            var eventCalled = false;

            IReflectionCache cache = new ReflectionCache();
            IBinder binder = new Binder();
            IInjector injector = new Injector(cache, binder);
            var instanceToInject = new MockClassVerySimple();

            injector.beforeInject += delegate(IInjector source, ref object instance, ReflectedClass reflectedClass) {
                //The if below is just to avoid checking when injecting on MockIClass.
                if (reflectedClass.type != typeof(MockClassVerySimple)) return;

                Assert.AreEqual(injector, source);
                Assert.AreEqual(instanceToInject, instance);
                Assert.IsNull(instanceToInject.field);

                eventCalled = true;
            };

            binder.Bind<IMockInterface>().To<MockIClass>();
            injector.Inject(instanceToInject);

            Assert.IsTrue(eventCalled);
            Assert.AreEqual(typeof(MockIClass), instanceToInject.field.GetType());
        }
Exemplo n.º 2
0
        public void TestAfterResolve()
        {
            var eventCalled = false;

            IReflectionCache cache = new ReflectionCache();
            IBinder binder = new Binder();
            IInjector injector = new Injector(cache, binder);
            IMockInterface resolvedInstance = null;

            injector.afterResolve += delegate(IInjector source,
                Type type,
                InjectionMember member,
                object parentInstance,
              	object identifier,
                ref object resolutionInstance) {
                Assert.AreEqual(injector, source);
                Assert.AreEqual(typeof(IMockInterface), type);
                Assert.AreEqual(InjectionMember.None, member);
                Assert.IsNull(parentInstance);
                Assert.IsNull(identifier);
                Assert.IsNotNull(resolutionInstance);

                resolvedInstance = (IMockInterface)resolutionInstance;
                eventCalled = true;

                return false;
            };

            binder.Bind<IMockInterface>().To<MockIClass>();
            var instance = injector.Resolve<IMockInterface>();

            Assert.IsTrue(eventCalled);
            Assert.AreEqual(typeof(MockIClass), instance.GetType());
            Assert.AreEqual(resolvedInstance, instance);
        }
Exemplo n.º 3
0
        public void TestCacheAll()
        {
            var cache = new ReflectionCache();

            cache.CacheFromBinder(this.binder);

            Assert.True(cache.Contains(typeof(MockIClassWithAttributes)));
            Assert.True(cache.Contains(typeof(MockClassWithDependencies)));
        }
Exemplo n.º 4
0
        public void TestAddType()
        {
            var cache = new ReflectionCache();
            var type = typeof(MockIClassWithoutAttributes);

            cache.Add(type);

            Assert.True(cache.Contains(type));
        }
Exemplo n.º 5
0
        public void TestBindingEvaluation()
        {
            var eventCalled = false;

            IReflectionCache cache = new ReflectionCache();
            IBinder binder = new Binder();
            IInjector injector = new Injector(cache, binder);

            injector.bindingEvaluation += delegate(IInjector source, ref BindingInfo binding) {
                Assert.AreEqual(injector, source);
                Assert.NotNull(binding);

                eventCalled = true;

                return new MockIClassWithoutAttributes();
            };

            binder.Bind<IMockInterface>().To<MockIClass>();
            var instance = injector.Resolve<IMockInterface>();

            Assert.IsTrue(eventCalled);
            Assert.AreEqual(typeof(MockIClassWithoutAttributes), instance.GetType());
        }