Esempio n. 1
0
        public ProxyFactoryTests()
        {
            _proxiedObj = new Mock <ITest>();
            _proxiedObj.Setup(i => i.Direct(1)).Returns("test");
            _proxiedObj.Setup(i => i.Intercepted(1)).Returns("test");
            _proxiedObj.Setup(i => i.InterceptedAsync(1)).ReturnsAsync("test");

            _concatenateInterceptor = MockMethodInterceptor((getNext, name, args, instance) =>
            {
                var result = getNext().Execute <string>(getNext, name, args, instance);
                return(result == null ? null
                    : (result + "_" + name + "_" + args.ArgumentPairs.Single().Key + ":" +
                       args.ArgumentPairs.Single().Value));
            });
            _reverseInterceptor = MockMethodInterceptor((getNext, name, args, instance) =>
            {
                var result = getNext().Execute <string>(getNext, name, args, instance);
                return(result == null ? null : string.Concat(result.Reverse()));
            });

            _interceptorManager = new TestInterceptorManager();
        }
Esempio n. 2
0
        public void Should_be_able_to_create_multiple_factories_of_the_same_type()
        {
            var interceptorSource   = new TestInterceptorSource(new[] { _concatenateInterceptor.Object });
            var interceptorLocator1 = new TestInterceptorManager();
            var interceptorLocator2 = new TestInterceptorManager();
            var proxyFactory1       = new ProxyFactory <ITest>(interceptorSource, interceptorLocator1);
            var proxyFactory2       = new ProxyFactory <ITest>(interceptorSource, interceptorLocator2);
            var proxy1 = proxyFactory1.GenerateProxy(_proxiedObj.Object);
            var proxy2 = proxyFactory2.GenerateProxy(_proxiedObj.Object);

            string returnedValue1 = proxy1.Intercepted(1);
            string returnedValue2 = proxy2.Intercepted(1);

            _proxiedObj.Verify(i => i.Intercepted(1), Times.Exactly(2));
            _concatenateInterceptor.Verify(
                h =>
                h.Execute <string>(It.IsAny <Func <IMethodInterceptor> >(), "Intercepted", It.IsAny <MethodArgs>(),
                                   It.IsAny <object>()), Times.Exactly(2));
            Assert.True(interceptorSource.CalledFindMatchingInterceptors);
            Assert.Equal("test_Intercepted_foo:1", returnedValue1);
            Assert.Equal("test_Intercepted_foo:1", returnedValue2);
        }