public void CanInterceptMethodsWithRefValue()
        {
            CountInterceptor.Reset();
            using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
            {
                var binding = kernel.Bind <RefAndOutValues>().ToSelf();
                binding.Intercept().With <CountInterceptor>();
                var foo = kernel.Get <RefAndOutValues>();
                int x   = 2;

                foo.Add(1, ref x, 2);

                CountInterceptor.Count.Should().Be(1);
                x.Should().Be(3);
            }
        }
Пример #2
0
        public void Equals_WhenUsingInterfaceProxy_IsInterceptable()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                CountInterceptor.Reset();

                kernel.Bind <IFoo>().To <NoneVirtualFooImplementation>().Intercept().With <CountInterceptor>();
                var obj      = kernel.Get <IFoo>();
                var result42 = obj.Equals(42);
                var result41 = obj.Equals(41);

                result42.Should().BeTrue();
                result41.Should().BeFalse();
                CountInterceptor.Count.Should().Be(2);
            }
        }
Пример #3
0
        public void ServiceBoundTypesDeclaringMethodInterceptorsAreIntercepted()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                kernel.Bind <IFoo>().To <ObjectWithMethodInterceptor>();
                var obj = kernel.Get <IFoo>();
                obj.Should().NotBeNull();
                typeof(IProxy).IsAssignableFrom(obj.GetType()).Should().BeTrue();

                CountInterceptor.Reset();

                obj.Foo();

                CountInterceptor.Count.Should().Be(1);
            }
        }
        public void MethodsFromDerivedClassesCanBeIntercepted()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                CountInterceptor.Reset();

                kernel.Bind <IDerived>().To <Derived>();

                var obj = kernel.Get <IDerived>();

                obj.DoBase();
                CountInterceptor.Count.Should().Be(1);

                obj.DoDerived();
                CountInterceptor.Count.Should().Be(1);
            }
        }
        public void CanAttachMultipleInterceptors()
        {
            FlagInterceptor.Reset();
            CountInterceptor.Reset();
            using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
            {
                var binding = kernel.Bind <FooImpl>().ToSelf();
                binding.Intercept().With <FlagInterceptor>();
                binding.Intercept().With <CountInterceptor>();
                var foo = kernel.Get <FooImpl>();

                foo.Foo();

                FlagInterceptor.WasCalled.Should().BeTrue();
                CountInterceptor.Count.Should().Be(1);
            }
        }
Пример #6
0
        public void ServiceBoundTypesDeclaringMethodInterceptorsAreIntercepted()
        {
            var testModule = new InlineModule(m => m.Bind <IFoo>().To <ObjectWithMethodInterceptor>());

            using (var kernel = new StandardKernel(new LinFuModule(), testModule))
            {
                var obj = kernel.Get <IFoo>();
                Assert.That(obj, Is.Not.Null);
                Assert.That(obj, Is.InstanceOfType(typeof(IProxy)));

                CountInterceptor.Reset();

                obj.Foo();

                Assert.That(CountInterceptor.Count, Is.EqualTo(1));
            }
        }
Пример #7
0
        public void InterceptUsedForWeaponPropertyInvoke()
        {
            var k = new StandardKernel(new NinjectSettings {
                LoadExtensions = false
            });

            k.Load <LinFuModule>();
            k.Bind <IWeapon>().To <Shuriken>().Intercept().With <CountInterceptor>();
            k.Bind <Samurai>().ToSelf();

            CountInterceptor.Reset();
            var samurai = k.Get <Samurai>();

            samurai.Attack("intruder");

            CountInterceptor.Count.Should().Be(1);
        }
Пример #8
0
        public void SelfBoundTypesDeclaringPropertyInterceptorsAreIntercepted()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                kernel.Bind <ObjectWithMethodInterceptor>().ToSelf();
                var obj = kernel.Get <ObjectWithMethodInterceptor>();

                CountInterceptor.Reset();

                var value = obj.TestProperty;
                obj.TestProperty = value;
                CountInterceptor.Count.Should().Be(0);

                var value2 = obj.TestProperty2;
                obj.TestProperty2 = value2;
                CountInterceptor.Count.Should().Be(1);
            }
        }
Пример #9
0
        public void NoneVirtualPropertyIntercepted_WhenResolveByInterface_ThenInterceptabe()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                CountInterceptor.Reset();

                const bool OriginalValue = true;
                kernel.Bind <IFoo>().To <NoneVirtualFooImplementation>();
                kernel.Intercept(ctx => ctx.Request.Service == typeof(IFoo)).With <CountInterceptor>();
                var obj = kernel.Get <IFoo>();

                obj.TestProperty = OriginalValue;
                var value = obj.TestProperty;

                CountInterceptor.Count.Should().Be(1);
                value.Should().Be(OriginalValue);
            }
        }
Пример #10
0
        public void SelfBoundTypesDeclaringMethodInterceptorsAreIntercepted()
        {
            var testModule = new InlineModule(m => m.Bind <ObjectWithMethodInterceptor>().ToSelf());

            using (var kernel = new StandardKernel(new DynamicProxy2Module(), testModule))
            {
                var obj = kernel.Get <ObjectWithMethodInterceptor>();
                Assert.That(obj, Is.Not.Null);
                Assert.That(obj, Is.InstanceOfType(typeof(IProxyTargetAccessor)));

                CountInterceptor.Reset();

                obj.Foo();
                obj.Bar();

                Assert.That(CountInterceptor.Count, Is.EqualTo(1));
            }
        }