public void SpecifyInterceptors(ISpecificationRegistry registry) { var matchDaoInsert = new AllOverloadsMatch(Method.Of<Model, int>(Dao.Insert)); var surround = new CompoundInterceptor(matchDaoInsert, new TraceEntry(), new InvokeOriginalCall(), new TraceExit()); registry.AddInterceptor(surround); }
public void SpecifyInterceptors(ISpecificationRegistry registry) { VoidAction<IInvocation> interceptor = i => { Console.WriteLine("BEFORE CALL."); i.Arguments[0] = 888; }; var compoundInterceptor = new CompoundInterceptor(new AlwaysMatches(), new BeforeCall(() => interceptor)); registry.AddInterceptor(compoundInterceptor); }
public void ArgumentsAreReplacedAndCallIsReplaced() { Function<int, string> replacementMethod = number => String.Format("Intercepted: {0}", number); Function<IList<object>, IList<object>> replaceArgs = args => new List<object> { 4444 }; var compoundInterceptor = new CompoundInterceptor(new AlwaysMatches(), new ReplaceArguments(() => replaceArgs), new InsteadOfCall(() => replacementMethod) ); InterceptorRegistry.AddInterceptor(compoundInterceptor); var mocked = new CodeUnderTest(); var result = mocked.CallsStringReturnOneParameter(); Assert.AreEqual("Intercepted: 4444", result); }
public void LogicIsInsertedAfter() { VoidAction<IInvocation> interceptor = i => { Console.WriteLine("AFTER " + i.OriginalCall.Method.Name); Console.WriteLine("METHOD RETURNED: {0}", i.Return); }; var compoundInterceptor = new CompoundInterceptor(new AlwaysMatches(), new AfterCall(() => interceptor)); InterceptorRegistry.AddInterceptor(compoundInterceptor); var mocked = new CodeUnderTest(); var result = mocked.CallsStringReturnOneParameter(); Assert.AreEqual("|| Original method return value when passed '999'. ||", result); }
public void MethodIsReplacedAndOriginalIsCalled() { VoidAction<IInvocation> interceptor = i => { var numberTimesTwo = (int)i.Arguments[0] * 2; var originalResult = i.OriginalCall.DynamicInvoke(numberTimesTwo); i.Return = String.Format("Intercepted: {0}", originalResult); }; var compoundInterceptor = new CompoundInterceptor( new AlwaysMatches(), new InvokeWithInvocation(() => interceptor) ); InterceptorRegistry.AddInterceptor(compoundInterceptor); var mocked = new CodeUnderTest(); var result = mocked.CallsStringReturnOneParameter(); Assert.AreEqual("Intercepted: || Original method return value when passed '1998'. ||", result); }