Example #1
0
        public void EmitDynamicDecoratorTypeWithDecorateeAndInterceptorDependency()
        {
            // Given
            using var iocContainer = new ServiceContainer();
            iocContainer.RegisterAssembly(typeof(IDynamicProxyFactory).Assembly);
            var proxyFactory        = iocContainer.GetInstance <IDynamicProxyFactory>();
            var expectedInterceptor = new LooseMockInterceptor(new ArrangementCollection());
            var expectedDecoratee   = new Foo();

            // When
            var instance = proxyFactory.CreateDecorator <IFoo>(expectedDecoratee, expectedInterceptor);

            // Then
            Assert.NotNull(instance);

            var decorateeField = instance?.GetType().GetField("_decoratee", BindingFlags.Instance | BindingFlags.NonPublic);

            Assert.NotNull(decorateeField);
            var actualDecoratee = decorateeField?.GetValue(instance);

            Assert.NotNull(actualDecoratee);
            Assert.Equal(expectedDecoratee, actualDecoratee);

            var interceptorField = instance?.GetType().GetField("_interceptor", BindingFlags.Instance | BindingFlags.NonPublic);

            Assert.NotNull(interceptorField);
            var actualInterceptor = interceptorField?.GetValue(instance);

            Assert.NotNull(actualInterceptor);
            Assert.Equal(expectedInterceptor, actualInterceptor);
        }
        public void SuccessfullInvocationWithoutArrangement()
        {
            // Given
            var type             = typeof(object);
            var methodName       = nameof(object.GetHashCode);
            var signature        = type.GetMethod(methodName) ?? throw new MethodInfoException(type, methodName);
            var methodInvocation = new Invocation(signature, new ReturnValueInvocation <int>());

            var emptyArrangements = new ArrangementCollection();
            var interceptor       = new LooseMockInterceptor(emptyArrangements);

            // When
            var wasIntercepted = interceptor.Intercept(methodInvocation);

            // Then
            Assert.True(wasIntercepted);
            var hasFeature = methodInvocation.TryGetFeature <IReturnValue <int> >(out var feature);

            Assert.True(hasFeature);
            Assert.Equal(default(int), feature?.ReturnValue);
        }