コード例 #1
0
ファイル: Mock.cs プロジェクト: belav/moq4
        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.Validate(invocationCount))
            {
                foreach (var(invocation, part) in invocationsToBeMarkedAsVerified)
                {
                    part.SetupEvaluatedSuccessfully(invocation);
                    invocation.MarkAsVerified();
                }
            }
            else
            {
                throw MockException.NoMatchingCalls(
                          mock,
                          expression,
                          failMessage,
                          times,
                          invocationCount
                          );
            }
        }
コード例 #2
0
ファイル: Mock.cs プロジェクト: tomchavakis/moq4
        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;
        }
コード例 #3
0
        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;
        }