public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { // Track current invocation if we're in "record" mode in a fluent invocation context. if (FluentMockContext.IsActive) { FluentMockContext.Current.Add(ctx.Mock, invocation); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { ctx.CurrentCall = FluentMockContext.IsActive ? (IProxyCall)null : ctx.OrderedCalls.LastOrDefault(c => c.Matches(invocation)); if (ctx.CurrentCall == null && !FluentMockContext.IsActive && ctx.Behavior == MockBehavior.Strict) { throw new MockException(MockException.ExceptionReason.NoSetup, ctx.Behavior, invocation); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { this.ctx = ctx; if (!FluentMockContext.IsActive) { //Special case for events if (invocation.Method.IsEventAttach()) { var delegateInstance = (Delegate)invocation.Arguments[0]; // TODO: validate we can get the event? var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(4)); if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface) { invocation.InvokeBase(); } else if (delegateInstance != null) { this.AddEventHandler(eventInfo, (Delegate)invocation.Arguments[0]); } return(InterceptionAction.Stop); } else if (invocation.Method.IsEventDetach()) { var delegateInstance = (Delegate)invocation.Arguments[0]; // TODO: validate we can get the event? var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(7)); if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface) { invocation.InvokeBase(); } else if (delegateInstance != null) { this.RemoveEventHandler(eventInfo, (Delegate)invocation.Arguments[0]); } return(InterceptionAction.Stop); } // Save to support Verify[expression] pattern. // In a fluent invocation context, which is a recorder-like // mode we use to evaluate delegates by actually running them, // we don't want to count the invocation, or actually run // previous setups. ctx.ActualInvocations.Add(invocation); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { if (invocation.Method.DeclaringType.IsGenericType && invocation.Method.DeclaringType.GetGenericTypeDefinition() == typeof(IMocked <>)) { // "Mixin" of IMocked<T>.Mock invocation.ReturnValue = ctx.Mock; return(InterceptionAction.Stop); } else if (invocation.Method.DeclaringType == typeof(IMocked)) { // "Mixin" of IMocked.Mock invocation.ReturnValue = ctx.Mock; return(InterceptionAction.Stop); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { if (invocation.Method != null && invocation.Method.ReturnType != null && invocation.Method.ReturnType != typeof(void)) { Mock recursiveMock; if (ctx.Mock.InnerMocks.TryGetValue(invocation.Method, out recursiveMock)) { invocation.ReturnValue = recursiveMock.Object; } else { invocation.ReturnValue = ctx.Mock.DefaultValueProvider.ProvideDefault(invocation.Method); } return(InterceptionAction.Stop); } return(InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { if (invocation.Method != null && invocation.Method.ReturnType != null && invocation.Method.ReturnType != typeof(void)) { Mock recursiveMock; if (ctx.Mock.InnerMocks.TryGetValue(invocation.Method, out recursiveMock)) { invocation.ReturnValue = recursiveMock.Object; } else { invocation.ReturnValue = ctx.Mock.DefaultValueProvider.ProvideDefault(invocation.Method); } return InterceptionAction.Stop; } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { this.ctx = ctx; if (ctx.CurrentCall != null) { ctx.CurrentCall.SetOutParameters(invocation); // We first execute, as there may be a Throws // and therefore we might never get to the // next line. ctx.CurrentCall.Execute(invocation); ThrowIfReturnValueRequired(ctx.CurrentCall, invocation); return(InterceptionAction.Stop); } else { return(InterceptionAction.Continue); } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { this.ctx = ctx; if (ctx.CurrentCall != null) { ctx.CurrentCall.SetOutParameters(invocation); // We first execute, as there may be a Throws // and therefore we might never get to the // next line. ctx.CurrentCall.Execute(invocation); ThrowIfReturnValueRequired(ctx.CurrentCall, invocation); return InterceptionAction.Stop; } else { return InterceptionAction.Continue; } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { if (invocation.Method.DeclaringType == typeof(object) || invocation.Method.DeclaringType.IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase ) { // Invoke underlying implementation. // For mocked classes, if the target method was not abstract, // invoke directly. // Will only get here for Loose behavior. // TODO: we may want to provide a way to skip this by the user. invocation.InvokeBase(); return(InterceptionAction.Stop); } else { return(InterceptionAction.Continue); } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { if (invocation.Method.DeclaringType == typeof(object) || invocation.Method.DeclaringType.IsClass && !invocation.Method.IsAbstract && ctx.Mock.CallBase ) { // Invoke underlying implementation. // For mocked classes, if the target method was not abstract, // invoke directly. // Will only get here for Loose behavior. // TODO: we may want to provide a way to skip this by the user. invocation.InvokeBase(); return InterceptionAction.Stop; } else { return InterceptionAction.Continue; } }
public void Intercept(ICallContext invocation) { lock (Mock) // this solves issue #249 { var interceptionContext = new InterceptStrategyContext(Mock , targetType , invocationLists , actualInvocations , behavior , orderedCalls ); foreach (var strategy in InterceptionStrategies()) { if (InterceptionAction.Stop == strategy.HandleIntercept(invocation, interceptionContext)) { break; } } } }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { // Track current invocation if we're in "record" mode in a fluent invocation context. if (FluentMockContext.IsActive) { FluentMockContext.Current.Add(ctx.Mock, invocation); } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { return invocation.Method.IsDestructor() ? InterceptionAction.Stop : InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { return(invocation.Method.IsDestructor() ? InterceptionAction.Stop : InterceptionAction.Continue); }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { this.ctx = ctx; if (!FluentMockContext.IsActive) { //Special case for events if (invocation.Method.IsEventAttach()) { var delegateInstance = (Delegate)invocation.Arguments[0]; // TODO: validate we can get the event? var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(4)); if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface) { invocation.InvokeBase(); } else if (delegateInstance != null) { this.AddEventHandler(eventInfo, (Delegate)invocation.Arguments[0]); } return InterceptionAction.Stop; } else if (invocation.Method.IsEventDetach()) { var delegateInstance = (Delegate)invocation.Arguments[0]; // TODO: validate we can get the event? var eventInfo = this.GetEventFromName(invocation.Method.Name.Substring(7)); if (ctx.Mock.CallBase && !eventInfo.DeclaringType.IsInterface) { invocation.InvokeBase(); } else if (delegateInstance != null) { this.RemoveEventHandler(eventInfo, (Delegate)invocation.Arguments[0]); } return InterceptionAction.Stop; } // Save to support Verify[expression] pattern. // In a fluent invocation context, which is a recorder-like // mode we use to evaluate delegates by actually running them, // we don't want to count the invocation, or actually run // previous setups. ctx.ActualInvocations.Add(invocation); } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { if (invocation.Method.DeclaringType.IsGenericType && invocation.Method.DeclaringType.GetGenericTypeDefinition() == typeof(IMocked<>)) { // "Mixin" of IMocked<T>.Mock invocation.ReturnValue = ctx.Mock; return InterceptionAction.Stop; } else if (invocation.Method.DeclaringType == typeof(IMocked)) { // "Mixin" of IMocked.Mock invocation.ReturnValue = ctx.Mock; return InterceptionAction.Stop; } return InterceptionAction.Continue; }
public InterceptionAction HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx) { ctx.CurrentCall = FluentMockContext.IsActive ? (IProxyCall)null : ctx.OrderedCalls.LastOrDefault(c => c.Matches(invocation)); if (ctx.CurrentCall == null && !FluentMockContext.IsActive && ctx.Behavior == MockBehavior.Strict) { throw new MockException(MockException.ExceptionReason.NoSetup, ctx.Behavior, invocation); } return InterceptionAction.Continue; }