/// <summary> /// Specifies that the arranged member will return consecutive values from the given array. /// If the arranged member is called after it has returned the last value, the behavior depends on the behavior parameter. /// </summary> /// <typeparam name="TReturn">Type of return value</typeparam> /// <param name="func">The arranged member</param> /// <param name="values">The list of values that will be returned by the arranged member. The list may be modified after the arrangement is made.</param> /// <param name="behavior">The behavior after the last value has been returned.</param> /// <returns>Reference to <see cref="IAssertable"/> interface.</returns> public static IAssertable ReturnsMany <TReturn>(this IFunc <TReturn> func, IList <TReturn> values, AfterLastValue behavior) { return(ProfilerInterceptor.GuardInternal(() => { if (values == null || values.Count == 0) { throw new ArgumentException("Expected at least one value to return", "values"); } Action <ReturnsManyImpl <TReturn> > afterEndAction = null; switch (behavior) { case AfterLastValue.ThrowAssertionFailed: afterEndAction = impl => MockingContext.Fail("List of arranged return values exhausted."); break; case AfterLastValue.KeepReturningLastValue: afterEndAction = impl => impl.CurrentIndex = values.Count - 1; break; case AfterLastValue.StartFromBeginning: afterEndAction = impl => impl.CurrentIndex = 0; break; default: throw new ArgumentException("behavior"); } return func.Returns(new ReturnsManyImpl <TReturn>(values, afterEndAction).GetNext); })); }
public void Assert() { if (!IsExpectationMet) { MockingContext.Fail("Calls should be executed in the order they are expected. Actual order of calls:\n{0}", InOrderExecutionMessage); } }
private static bool DispatchInvocation(Invocation invocation) { DebugView.TraceEvent(IndentLevel.Dispatch, () => String.Format("Intercepted profiler call: {0}", invocation.InputToString())); DebugView.PrintStackTrace(); var mockMixin = invocation.MockMixin; var repo = mockMixin != null ? mockMixin.Repository : MockingContext.ResolveRepository(UnresolvedContextBehavior.CreateNewContextual); if (repo == null) { repo = TryFindGlobalInterceptor(invocation.Method); } if (repo == null) { return(false); } 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 Assert() { if (this.strictnessViolationMessage != null) { MockingContext.Fail(this.strictnessViolationMessage.ToString()); } }
public void Assert() { if (!this.IsMet) { MockingContext.Fail("Not all prerequisites are met. Actual call processed:\n{0}", this.ExecutionMessage); } }
public void Assert() { if (!IsExpectationMet) { MockingContext.Fail("{0}Calls should be executed in the order they are expected. Actual order of calls:\n{1}", this.message != null ? this.message + " " : "", InOrderExecutionMessage); } }
public void ForEachMock(Action <object> action) { using (MockingContext.BeginFailureAggregation(null)) { foreach (var mock in this.mocks) { action(mock); } } }
public void Process(Invocation invocation) { this.wasCalled = true; this.calledInWrongOrder = (this.LastIdInOrder != this.arrangementId - 1); this.LastIdInOrder = this.arrangementId; this.InOrderExecutionLog += invocation.InputToString() + " called at:\n" + MockingContext.GetStackTrace(" "); if (this.calledInWrongOrder) { MockingContext.Fail("Last call executed out of order. Order of calls so far:\n{1}", invocation.InputToString(), InOrderExecutionMessage); } }
public void TraceEvent(string message) { #if !PORTABLE if ((this.TraceOptions & TraceOptions.RemoteTrace) != 0) { try { if (MockingContext.Plugins.Exists <IDebugWindowPlugin>()) { // traces triggered by profiler intercepted calls and repository retirement // could cause deadlocks and infinite loops in remote tracing, so skip them var testMethod = MockingContext.GetTestMethod(); var repo = MockingContext.ResolveRepository(UnresolvedContextBehavior.DoNotCreateNew); if (testMethod != null && repo != null && !repo.IsRetired) { var debugWindowPlugin = MockingContext.Plugins.Get <IDebugWindowPlugin>(); debugWindowPlugin.TraceMessage(message); } } } catch (Exception e) { System.Diagnostics.Trace.WriteLine("Exception thrown calling IDebugWindowPlugin plugin: " + e); } } #endif if ((this.TraceOptions & TraceOptions.InternalTrace) != 0) { Debug.WriteLine(message); lock (this.traceSync) { this.log.AppendLine(message); if (this.currentTraceRead) { this.currentTraceRead = false; this.currentTrace.Length = 0; } this.currentTrace.AppendLine(message); } } }
public static void Assert(int?lowerBound, int?upperBound, int calls, object expression) { if (IsInRange(lowerBound, upperBound, calls)) { return; } var message = String.Format("Occurrence expectation failed. {0}. Calls so far: {1}", MakeRangeString(lowerBound, upperBound), calls); if (expression != null) { message += String.Format("\nArrange expression: {0}", expression).EscapeFormatString(); } MockingContext.Fail(message); }
public void Process(Invocation invocation) { this.wasCalled = true; bool processedOnce = this.LastIdInOrder > -1; this.calledInWrongOrder = processedOnce && (this.LastIdInOrder - this.arrangementId) > 0 || (this.LastIdInOrder - this.arrangementId) < -1; this.LastIdInOrder = this.arrangementId; this.InOrderExecutionLog += invocation.InputToString() + " called at:\n" + MockingContext.GetStackTrace(" "); if (this.calledInWrongOrder) { MockingContext.Fail("{0}Last call executed out of order. Order of calls so far:\n{1}", this.message != null ? this.message + " " : "", InOrderExecutionMessage); } }
public void Process(Invocation invocation) { if (invocation.Recording || invocation.InArrange || invocation.InAssertSet) { return; } var returnType = invocation.Method.GetReturnType(); if (!typeof(Task).IsAssignableFrom(returnType)) { MockingContext.Fail("Wrong invocation to arrangement: return type of {0}.{1} is not a task", invocation.Instance != null ? MockingUtil.GetUnproxiedType(invocation.Instance) : invocation.Method.DeclaringType, invocation.Method.Name); } var elementType = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>) ? returnType.GetGenericArguments()[0] : typeof(object); Expression <Func <Task <object> > > taskFromException = () => MockingUtil.TaskFromException <object>((Exception)null); var mock = ((MethodCallExpression)taskFromException.Body).Method .GetGenericMethodDefinition() .MakeGenericMethod(elementType) .Invoke(null, new object[] { this.exception }); var parentMock = invocation.MockMixin; var mockMixin = MocksRepository.GetMockMixin(mock, null); if (parentMock != null && mockMixin != null) { parentMock.DependentMocks.Add(mock); } invocation.ReturnValue = mock; invocation.CallOriginal = false; invocation.UserProvidedImplementation = true; }
internal static void PrintStackTrace() { TraceEvent(IndentLevel.StackTrace, () => "Stack trace:\n" + MockingContext.GetStackTrace(IndentLevel.StackTraceInner.AsIndent())); }
/// <summary> /// Removes all existing arrangements within the current mocking context (e.g. current test method). /// Arrangements made in parent mocking contexts (e.g. in fixture setup method) are preserved. /// </summary> public static void Reset() { ProfilerInterceptor.GuardInternal(() => MockingContext.RetireRepository()); }
public void Process(Invocation invocation) { this.processedStackTrace = invocation.InputToString() + " called at:\n" + MockingContext.GetStackTrace(" "); }