public void An_unsuccessful_invocation_calls_OnBefore__OnFail_and_OnAfter_respectively() { var testing = new TInterceptor(); invocation.FailsWith(new Exception()); Assert.Throws <Exception>(() => invocation.Intercept(testing)); Assert.IsTrue((bool)invocation.Context["before"]); Assert.IsNull(invocation.Context["invocation"]); Assert.IsNull(invocation.Context["success"]); Assert.IsTrue((bool)invocation.Context["fail"]); Assert.IsTrue((bool)invocation.Context["after"]); }
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)); }
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); }
public void When_invocation_fails_all_interceptors_work_in_the_reverse_order__since_first_wraps_second() { var interceptor = new ChainInterceptor <Context>(); interceptor.Add(BuildRoutine.Interceptor <Context>().Do() .Before(ctx => ctx.Value += " - before1") .Fail(ctx => ctx.Value += " - fail1") .After(ctx => ctx.Value += " - after1")); interceptor.Add(BuildRoutine.Interceptor <Context>().Do() .Before(ctx => ctx.Value += " - before2") .Fail(ctx => ctx.Value += " - fail2") .After(ctx => ctx.Value += " - after2")); var testing = interceptor; invocation.Context.Value = "begin"; invocation.FailsWith(new Exception()); Assert.Throws <Exception>(() => invocation.Intercept(testing)); Assert.AreEqual("begin - before1 - before2 - fail2 - after2 - fail1 - after1", invocation.Context.Value); Assert.AreEqual(1, invocation.Count); }