public void CanInterceptSpecifiedMethods()
        {
            var spec = new TestSpecification();
            spec.SpecifyInterceptors(new SpecificationRegistry());

            var mocked = new CodeUnderTest();
            var result = mocked.CallsStringReturnOneParameter();

            Assert.AreEqual("|| Original method return value when passed '888'. ||", result);
        }
        public void ArgumentsAreReplaced()
        {
            Function<IList<object>, IList<object>> replace = args => new List<object> { 5555 };

            InterceptorRegistry.AddInterceptor(
                new CompoundInterceptor(new AlwaysMatches(),
                    new ReplaceArguments(() => replace),
                    new InvokeOriginalCall()
                    ));

            var mocked = new CodeUnderTest();
            var result = mocked.CallsStringReturnOneParameter();

            Assert.AreEqual("|| Original method return value when passed '5555'. ||", result);
        }
        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);
        }