private static bool DispatchInvocation(Invocation invocation) { var mockMixin = MocksRepository.GetMockMixinFromInvocation(invocation); var repo = mockMixin != null ? mockMixin.Repository : MockingContext.ResolveRepository(UnresolvedContextBehavior.CreateNewContextual); if (repo == null) { repo = TryFindGlobalInterceptor(invocation.Method); } if (repo == null) { return(false); } DebugView.TraceEvent(IndentLevel.Dispatch, () => String.Format("Intercepted profiler call: {0}", invocation.InputToString())); DebugView.PrintStackTrace(); lock (repo) { repo.DispatchInvocation(invocation); } if (invocation.CallOriginal) { DebugView.TraceEvent(IndentLevel.DispatchResult, () => "Calling original implementation"); } else if (invocation.IsReturnValueSet) { DebugView.TraceEvent(IndentLevel.DispatchResult, () => String.Format("Returning value '{0}'", invocation.ReturnValue)); } return(true); }
public void Process(Invocation invocation) { var method = invocation.Method; if (method is ConstructorInfo && method.IsPrivate && method.IsStatic) { var mixin = MocksRepository.GetMockMixinFromInvocation(invocation); invocation.CallOriginal = mixin == null || !mixin.IsStaticConstructorMocked; } }
public void Process(Invocation invocation) { var method = invocation.Method; if (method is ConstructorInfo && !method.IsStatic) { var mixin = MocksRepository.GetMockMixinFromInvocation(invocation); invocation.CallOriginal = mixin != null ? !mixin.IsInstanceConstructorMocked : true; invocation.UserProvidedImplementation = true; } }
public void Process(Invocation invocation) { var mockMixin = MocksRepository.GetMockMixinFromInvocation(invocation); if (mockMixin == null) { mockMixin = invocation.Repository.CreateExternalMockMixin(null, invocation.Instance, Behavior.CallOriginal); mockMixin.IsInstanceConstructorMocked = true; } invocation.CallOriginal = !mockMixin.IsInstanceConstructorMocked; }
public void Intercept(IInvocation invocation) { if (ProfilerInterceptor.ReentrancyCounter > 0) { CallOriginal(invocation, false); return; } bool callOriginal = false; ProfilerInterceptor.GuardInternal(() => { var mockInvocation = new Invocation(invocation.Proxy, invocation.GetConcreteMethod(), invocation.Arguments); DebugView.TraceEvent(IndentLevel.Dispatch, () => String.Format("Intercepted DP call: {0}", mockInvocation.InputToString())); DebugView.PrintStackTrace(); var mock = MocksRepository.GetMockMixinFromInvocation(mockInvocation); var repo = mock != null ? mock.Repository : this.constructionRepo; lock (repo) { repo.DispatchInvocation(mockInvocation); } invocation.ReturnValue = mockInvocation.ReturnValue; callOriginal = mockInvocation.CallOriginal; if (callOriginal) { DebugView.TraceEvent(IndentLevel.DispatchResult, () => "Calling original implementation"); } else if (mockInvocation.IsReturnValueSet) { DebugView.TraceEvent(IndentLevel.DispatchResult, () => String.Format("Returning value '{0}'", invocation.ReturnValue)); } }); if (callOriginal) { CallOriginal(invocation, true); } }
public void Process(Invocation invocation) { if (invocation.IsReturnValueSet) { return; } var returnType = invocation.Method.GetReturnType(); if (returnType == typeof(void) || returnType.IsValueType) { return; } if (invocation.Method.Name == "ToString" && invocation.Method.GetParameters().Length == 0 && invocation.UserProvidedImplementation) { return; } object mock = null; List <KeyValuePair <object, object> > mocksList; if (mocks.TryGetValue(invocation.Method, out mocksList)) { // can't put the key part in a Dictionary, // because we can't be sure that GetHashCode() works mock = mocksList.FirstOrDefault(kvp => Equals(kvp.Key, invocation.Instance)).Value; } if (mock == null) { var parentMock = MocksRepository.GetMockMixinFromInvocation(invocation); var repository = parentMock.Repository; var replicator = parentMock as IMockReplicator; bool mustReturnAMock = invocation.InArrange || this.type == RecursiveMockingBehaviorType.ReturnMock; if (mustReturnAMock || this.type == RecursiveMockingBehaviorType.ReturnDefault) { if (returnType.IsArray) { mock = Array.CreateInstance(returnType.GetElementType(), Enumerable.Repeat(0, returnType.GetArrayRank()).ToArray()); } var idictionaryType = returnType.GetImplementationOfGenericInterface(typeof(IDictionary <,>)); if (mock == null && idictionaryType != null) { var dictType = typeof(Dictionary <,>).MakeGenericType(idictionaryType.GetGenericArguments()); mock = MockCollection.Create(returnType, repository, replicator, (IEnumerable)MockingUtil.CreateInstance(dictType)); } var ienumerableType = returnType.GetImplementationOfGenericInterface(typeof(IEnumerable <>)); if (mock == null && ienumerableType != null) { var listType = typeof(List <>).MakeGenericType(ienumerableType.GetGenericArguments()); mock = MockCollection.Create(returnType, repository, replicator, (IEnumerable)MockingUtil.CreateInstance(listType)); } if (mock == null && mustReturnAMock) { #if !LITE_EDITION var stackTrace = new StackTrace(); var methodCallingArrange = stackTrace.EnumerateFrames() .SkipWhile(m => !Attribute.IsDefined(m, typeof(ArrangeMethodAttribute))) .SkipWhile(m => m.Module.Assembly == typeof(MocksRepository).Assembly) .FirstOrDefault(); if (methodCallingArrange != null && invocation.Method.DeclaringType.IsAssignableFrom(methodCallingArrange.DeclaringType)) { return; } #endif if (typeof(String) == returnType) { mock = String.Empty; } else { try { mock = replicator.CreateSimilarMock(repository, returnType, null, true, null); } catch (MockException) { } } } } if (mock == null) { return; } if (mocksList == null) { mocksList = new List <KeyValuePair <object, object> >(); mocks.Add(invocation.Method, mocksList); } mocksList.Add(new KeyValuePair <object, object>(invocation.Instance, mock)); var mockMixin = MocksRepository.GetMockMixin(mock, null); if (parentMock != null && mockMixin != null) { parentMock.DependentMocks.Add(mock); } } invocation.ReturnValue = mock; invocation.CallOriginal = false; invocation.UserProvidedImplementation = true; }