コード例 #1
0
        public void NamedBindingsUseTheCorrectInterceptor()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                CountInterceptor.Reset();
                FlagInterceptor.Reset();

                kernel.Bind <IFoo>().To <FooImpl>().Named("1");
                kernel.Bind <IFoo>().To <FooWithNoDefaultConstructor>().Named("2");
                kernel.Bind <IMock>().To <SimpleObject>();

                kernel.Intercept(ctx => ctx.Plan.Type == typeof(FooImpl)).With <CountInterceptor>();
                kernel.Intercept(ctx => ctx.Plan.Type == typeof(FooWithNoDefaultConstructor)).With <FlagInterceptor>();

                var foo1 = kernel.Get <IFoo>(ctx => ctx.Name == "1");
                var foo2 = kernel.Get <IFoo>(ctx => ctx.Name == "2");

                foo1.Foo();
                CountInterceptor.Count.Should().Be(1);
                FlagInterceptor.WasCalled.Should().BeFalse();

                foo2.Foo();
                CountInterceptor.Count.Should().Be(1);
                FlagInterceptor.WasCalled.Should().BeTrue();
            }
        }
 public void Doo()
 {
     FlagInterceptor.Reset();
     using (StandardKernel kernel = this.CreateDefaultInterceptionKernel())
     {
         var mock = kernel.Get <ViewModel>();
         mock.Address = "|ad";
         FlagInterceptor.WasCalled.Should().BeTrue();
     }
 }
        public void SelfBoundTypesDeclaringInterceptorsOnGenericMethodsAreIntercepted()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                kernel.Bind <ObjectWithGenericMethod>().ToSelf();
                var obj = kernel.Get <ObjectWithGenericMethod>();

                FlagInterceptor.Reset();
                string result = obj.ConvertGeneric("", 42);

                result.Should().Be("42");
                FlagInterceptor.WasCalled.Should().BeTrue();
            }
        }
コード例 #4
0
        public void SinsgletonTests()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                kernel.Bind <RequestsConstructorInjection>().ToSelf().InSingletonScope().Intercept().With <FlagInterceptor>();
                var obj = kernel.Get <RequestsConstructorInjection>();

                obj.Should().NotBeNull();
                typeof(IProxy).IsAssignableFrom(obj.GetType()).Should().BeTrue();
                FlagInterceptor.Reset();

                obj.Child.Should().NotBeNull();
                FlagInterceptor.WasCalled.Should().BeTrue();
            }
        }
コード例 #5
0
        public void SelfBoundTypesDeclaringInterceptorsOnGenericMethodsAreIntercepted()
        {
            using (var kernel = CreateDefaultInterceptionKernel())
            {
                kernel.Bind <ObjectWithGenericMethod>().ToSelf();
                var obj = kernel.Get <ObjectWithGenericMethod>();
                obj.Should().NotBeNull();
                typeof(IProxy).IsAssignableFrom(obj.GetType()).Should().BeTrue();

                FlagInterceptor.Reset();
                string result = obj.ConvertGeneric(42);

                result.Should().Be("42");
                FlagInterceptor.WasCalled.Should().BeTrue();
            }
        }
        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);
            }
        }
コード例 #7
0
        public void SelfBoundTypesDeclaringInterceptorsOnGenericMethodsAreIntercepted()
        {
            var testModule = new InlineModule(m => m.Bind <ObjectWithGenericMethod>().ToSelf());

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

                FlagInterceptor.Reset();

                string result = obj.ConvertGeneric(42);

                Assert.That(result, Is.EqualTo("42"));
                Assert.That(FlagInterceptor.WasCalled, Is.True);
            }
        }
        public void ServiceBoundTypesDeclaringInterceptorsOnGenericMethodsAreIntercepted()
        {
            using (StandardKernel kernel = CreateDefaultInterceptionKernel())
            {
                kernel.Bind <IGenericMethod>().To <ObjectWithGenericMethod>();
                var obj = kernel.Get <IGenericMethod>();

                obj.Should().NotBeNull();
                this.ProxyType.IsAssignableFrom(obj.GetType()).Should().BeTrue();

                FlagInterceptor.Reset();

                string result = obj.ConvertGeneric("", 42);

                result.Should().Be("42");
                FlagInterceptor.WasCalled.Should().BeTrue();
            }
        }