private static void VerifyCalls( Mock targetMock, MethodCall expected, LambdaExpression expression, Times times) { var allInvocations = targetMock.MutableInvocations.ToArray(); var matchingInvocations = allInvocations.Where(expected.Matches).ToArray(); var matchingInvocationCount = matchingInvocations.Length; if (!times.Verify(matchingInvocationCount)) { var setups = targetMock.Setups.ToArrayLive(oc => AreSameMethod(oc.SetupExpression, expression)); throw MockException.NoMatchingCalls(expected, setups, allInvocations, expression, times, matchingInvocationCount); } else { foreach (var matchingInvocation in matchingInvocations) { matchingInvocation.MarkAsVerified(); } } bool AreSameMethod(LambdaExpression l, LambdaExpression r) => l.Body is MethodCallExpression lc && r.Body is MethodCallExpression rc && lc.Method == rc.Method; }
private static void VerifyCalls( Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times) { var callCount = targetInterceptor.ActualCalls.Where(ac => expected.Matches(ac)).Count(); if (!times.Verify(callCount)) { var setups = targetInterceptor.OrderedCalls.Where(oc => AreSameMethod(oc.SetupExpression, expression)); ThrowVerifyException(expected, setups, targetInterceptor.ActualCalls, expression, times, callCount); } }
private static void VerifyCalls( Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times) { // .Where does an enumeration, and calls to a mocked method concurrent to VerifyCalls might change the content of ActualCalls. therefore, it is necessary to take a snapshot, using ToList(), so that concurrent calls will not impact the ongoing verification. var actualCalls = targetInterceptor.InterceptionContext.ActualInvocations.ToList(); var callCount = actualCalls.Where(ac => expected.Matches(ac)).Count(); if (!times.Verify(callCount)) { var setups = targetInterceptor.InterceptionContext.OrderedCalls.Where(oc => AreSameMethod(oc.SetupExpression, expression)); ThrowVerifyException(expected, setups, actualCalls, expression, times, callCount); } }
internal static void Verify(Mock mock, LambdaExpression expression, Times times, string failMessage) { Guard.NotNull(times, nameof(times)); var invocationCount = Mock.GetMatchingInvocationCount(mock, expression, out var invocationsToBeMarkedAsVerified); if (times.Verify(invocationCount)) { foreach (var invocation in invocationsToBeMarkedAsVerified) { invocation.MarkAsVerified(); } } else { throw MockException.NoMatchingCalls(mock, expression, failMessage, times, invocationCount); } }
private static void VerifyCalls( Mock targetMock, InvocationShape expectation, LambdaExpression expression, Times times, string failMessage) { var allInvocations = targetMock.MutableInvocations.ToArray(); var matchingInvocations = allInvocations.Where(expectation.IsMatch).ToArray(); var matchingInvocationCount = matchingInvocations.Length; if (!times.Verify(matchingInvocationCount)) { Setup[] setups; if (targetMock.IsDelegateMock) { // For delegate mocks, there's no need to compare methods as for regular mocks (below) // since there's only one single method, so include all setups unconditionally. setups = targetMock.Setups.ToArrayLive(s => true); } else { setups = targetMock.Setups.ToArrayLive(oc => AreSameMethod(oc.Expression, expression)); } throw MockException.NoMatchingCalls(failMessage, setups, allInvocations, expression, times, matchingInvocationCount); } else { foreach (var matchingInvocation in matchingInvocations) { matchingInvocation.MarkAsVerified(); } } bool AreSameMethod(LambdaExpression l, LambdaExpression r) => l.Body is MethodCallExpression lc && r.Body is MethodCallExpression rc && lc.Method == rc.Method; }