Ejemplo n.º 1
0
        public async Task InterceptionContextAsyncRegisterInsteadOfInterceptorsTest()
        {
            bool before1 = false, exception = false, after1 = false;

            var context = new AsyncInterceptionContext(this, "TestMethod", (Func <string, Task <string> >)TestMethodAsync, "TestValue");

            context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => before1 = true, InterceptionMode.BeforeBody));
            context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => c.SetResult("Result"), InterceptionMode.InsteadOfBody));
            try
            {
                context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => c.SetResult("whatever"), InterceptionMode.InsteadOfBody));
            }
            catch (ArgumentException ex)
            {
                exception = ex.ParamName == "interceptor";
            }
            context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => after1 = true, InterceptionMode.AfterBody));

            var result = await context.ExecuteAsync <string>();

            Assert.True(before1);
            Assert.True(exception);
            Assert.Equal("Result", result);
            Assert.True(after1);
        }
Ejemplo n.º 2
0
        public async Task InterCeptionContextInsteadOfWithValueTypes()
        {
            var context = new AsyncInterceptionContext(null, "test", new Func <Task <bool> >(async() => false));

            context.RegisterInterceptor(new DelegateAsyncMethodInterceptor(async c => c.SetResult(Activator.CreateInstance(c.ReturnType.GetGenericArguments()[0])), InterceptionMode.InsteadOfBody));

            var result = await context.ExecuteAsync <bool>();

            Assert.False(result);
        }