示例#1
0
        public void First_added_interceptor_wraps_the_second_one_and_invocation_happens_last()
        {
            var interceptor = new ChainInterceptor <Context>();

            interceptor.Add(BuildRoutine.Interceptor <Context>().Do()
                            .Before(ctx => ctx.Value  += " - before1")
                            .Success(ctx => ctx.Value += " - success1")
                            .After(ctx => ctx.Value   += " - after1"));
            interceptor.Add(BuildRoutine.Interceptor <Context>().Do()
                            .Before(ctx => ctx.Value  += " - before2")
                            .Success(ctx => ctx.Value += " - success2")
                            .After(ctx => ctx.Value   += " - after2"));

            var testing = interceptor;

            invocation.Context.Value = "begin";

            invocation.Returns("actual");

            var actual = invocation.Intercept(testing);

            Assert.AreEqual("actual", actual);

            Assert.AreEqual("begin - before1 - before2 - success2 - after2 - success1 - after1", invocation.Context.Value);
            Assert.AreEqual(1, invocation.Count);
        }
示例#2
0
        public void Decorates_an_invocation_with_a_variable_of_given_type_that_is_created_OnBefore()
        {
            var testing = builder.Build(
                before: () => "test string",
                success: actual => Assert.AreEqual("test string", actual),
                fail: actual => Assert.AreEqual("test string", actual),
                after: actual => Assert.AreEqual("test string", actual)
                );

            invocation.Intercept(testing);

            invocation.FailsWith(new Exception());

            Assert.Throws <Exception>(() => invocation.Intercept(testing));
        }
示例#3
0
    private object Intercept(int i, MethodInfo method, object[] args, Func <object> proceed)
    {
        IInvocation invocation = _invocations.ElementAtOrDefault(i);

        if (invocation == null)
        {
            return(proceed());
        }

        return(invocation.Intercept(method, args, () => Intercept(++i, method, args, proceed)));
    }
    private object Intercept(int i, string method, Func <object> proceed)
    {
        IInvocation invocation = _invocations.ElementAtOrDefault(i);

        if (invocation == null)
        {
            return(proceed());
        }

        return(invocation.Intercept(method, () => Intercept(++i, method, proceed)));
    }
        public void Before_success_fail_actions_can_be_defined_by_delegates()
        {
            invocation.Context.Value = "begin";

            var testing = builder.Build(
                before: () => invocation.Context.Value  += " - before",
                success: () => invocation.Context.Value += " - success",
                fail: () => invocation.Context.Value    += " - fail",
                after: () => invocation.Context.Value   += " - after"
                );

            invocation.Intercept(testing);

            Assert.AreEqual("begin - before - success - after", invocation.Context.Value);

            invocation.Context.Value = "begin";
            invocation.FailsWith(new Exception());

            Assert.Throws <Exception>(() => invocation.Intercept(testing));
            Assert.AreEqual("begin - before - fail - after", invocation.Context.Value);
        }
示例#6
0
        public void A_successful_invocation_calls_OnBefore__OnSuccess_and_OnAfter_respectively()
        {
            var testing = new TInterceptor();

            invocation.Returns("result");

            var actual = invocation.Intercept(testing);

            Assert.AreEqual("result", actual);

            Assert.IsTrue((bool)invocation.Context["before"]);
            Assert.IsTrue((bool)invocation.Context["invocation"]);
            Assert.IsTrue((bool)invocation.Context["success"]);
            Assert.IsNull(invocation.Context["fail"]);
            Assert.IsTrue((bool)invocation.Context["after"]);
            Assert.AreEqual(1, invocation.Count);
        }