Exemplo n.º 1
0
        /// <summary>
        ///   Returns the exception to be thrown when <see cref="Mock.Verify"/> finds no invocations (or the wrong number of invocations) that match the specified expectation.
        /// </summary>
        internal static MockException NoMatchingCalls(
            MethodCall expected,
            IEnumerable <MethodCall> setups,
            IEnumerable <Invocation> invocations,
            LambdaExpression expression,
            Times times,
            int callCount)
        {
            return(new MockException(
                       MockExceptionReason.NoMatchingCalls,
                       times.GetExceptionMessage(expected.FailMessage, expression.PartialMatcherAwareEval().ToStringFixed(), callCount) +
                       Environment.NewLine + FormatSetupsInfo() +
                       Environment.NewLine + FormatInvocations()));

            string FormatSetupsInfo()
            {
                var expressionSetups = setups.Select(s => s.Format()).ToArray();

                return(expressionSetups.Length == 0 ?
                       Resources.NoSetupsConfigured :
                       Environment.NewLine + string.Format(Resources.ConfiguredSetups, Environment.NewLine + string.Join(Environment.NewLine, expressionSetups)));
            }

            string FormatInvocations()
            {
                return(invocations.Any() ? Environment.NewLine + string.Format(Resources.PerformedInvocations, Environment.NewLine + string.Join <Invocation>(Environment.NewLine, invocations))
                                                         : Resources.NoInvocationsPerformed);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Returns the exception to be thrown when <see cref="Mock.Verify"/> finds no invocations (or the wrong number of invocations) that match the specified expectation.
        /// </summary>
        internal static MockException NoMatchingCalls(
            Mock rootMock,
            LambdaExpression expression,
            string failMessage,
            Times times,
            int callCount)
        {
            var message = new StringBuilder();

            message.AppendLine(times.GetExceptionMessage(failMessage, expression.PartialMatcherAwareEval().ToStringFixed(), callCount))
            .AppendLine(Resources.PerformedInvocations)
            .AppendLine();

            var visitedMocks = new HashSet <Mock>();

            var mocks = new Queue <Mock>();

            mocks.Enqueue(rootMock);

            while (mocks.Any())
            {
                var mock = mocks.Dequeue();

                if (visitedMocks.Contains(mock))
                {
                    continue;
                }
                visitedMocks.Add(mock);

                message.AppendLine(mock == rootMock ? $"   {mock} ({expression.Parameters[0].Name}):"
                                                                        : $"   {mock}:");

                var invocations = mock.MutableInvocations.ToArray();
                if (invocations.Any())
                {
                    message.AppendLine();
                    foreach (var invocation in invocations)
                    {
                        message.Append($"      {invocation}");

                        if (invocation.Method.ReturnType != typeof(void) && Unwrap.ResultIfCompletedTask(invocation.ReturnValue) is IMocked mocked)
                        {
                            var innerMock = mocked.Mock;
                            mocks.Enqueue(innerMock);
                            message.Append($"  => {innerMock}");
                        }

                        message.AppendLine();
                    }
                }
                else
                {
                    message.AppendLine($"   {Resources.NoInvocationsPerformed}");
                }

                message.AppendLine();
            }

            return(new MockException(MockExceptionReasons.NoMatchingCalls, message.TrimEnd().AppendLine().ToString()));
        }
Exemplo n.º 3
0
Arquivo: Mock.cs Projeto: lxf/moq4
        private static void ThrowVerifyException(
            MethodCall expected,
            IEnumerable <IProxyCall> setups,
            IEnumerable <ICallContext> actualCalls,
            Expression expression,
            Times times,
            int callCount)
        {
            var message = times.GetExceptionMessage(expected.FailMessage, expression.PartialMatcherAwareEval().ToLambda().ToStringFixed(), callCount) +
                          Environment.NewLine + FormatSetupsInfo(setups) +
                          Environment.NewLine + FormatInvocations(actualCalls);

            throw new MockException(MockException.ExceptionReason.VerificationFailed, message);
        }