public static void AssertMethodCallWithArguments <TMockObject>(this TMockObject mockObject, Expression <Action <TMockObject> > expectedMethodCallExpression)
            where TMockObject : MockObject
        {
            if (mockObject == null)
            {
                throw new ArgumentNullException("mockObject");
            }

            if (expectedMethodCallExpression == null)
            {
                throw new ArgumentNullException("expectedMethodCallExpression");
            }

            MethodCallExpressionVisitor visitor = new MethodCallExpressionVisitor();

            visitor.Visit(expectedMethodCallExpression);

            RecordedMethodCall[] recordedMethodCalls = mockObject.RecordedMethodCalls
                                                       .Where(c => c.Method == visitor.MethodCallInfo)
                                                       .ToArray();

            Type[] parameterTypes = visitor.MethodCallInfo.GetParameters()
                                    .Select(p => p.ParameterType)
                                    .ToArray();

            RecordedMethodCall methodCallMatch = recordedMethodCalls.FirstOrDefault(recordedMethodCall =>
                                                                                    UnitTestHelpers.CompareMethodCallArguments(parameterTypes, visitor.MethodCallArguments, recordedMethodCall.Arguments, mockObject.EqualityComparerFactory));

            Assert.IsNotNull(methodCallMatch, "Method '{0}' was not called.", visitor.MethodCallInfo);

            mockObject.RecordedMethodCalls.Remove(methodCallMatch);
        }
        private static RecordedMethodCall AssertMethodCallOnceInternal <TMockObject>(this TMockObject mockObject, Expression <Action <TMockObject> > expectedMethodCallExpression, bool compareArguments)
            where TMockObject : MockObject
        {
            if (mockObject == null)
            {
                throw new ArgumentNullException("mockObject");
            }

            if (expectedMethodCallExpression == null)
            {
                throw new ArgumentNullException("expectedMethodCallExpression");
            }

            MethodCallExpressionVisitor visitor = new MethodCallExpressionVisitor();

            visitor.Visit(expectedMethodCallExpression);

            RecordedMethodCall[] recordedMethodCalls;

            lock (mockObject.RecordedMethodCalls)
            {
                recordedMethodCalls = mockObject.RecordedMethodCalls
                                      .Where(c => c.Method == visitor.MethodCallInfo)
                                      .ToArray();
            }

            switch (recordedMethodCalls.Length)
            {
            case 0:
                Assert.Fail("Method '{0}' was not called.", visitor.MethodCallInfo.Name);
                break;

            case 1:
                break;

            default:
                Assert.Fail("Method '{0}' was called {1} times instead of 1.", visitor.MethodCallInfo.Name, recordedMethodCalls.Length);
                break;
            }

            RecordedMethodCall result = recordedMethodCalls.First();

            if (compareArguments)
            {
                Type[] parameterTypes = visitor.MethodCallInfo.GetParameters()
                                        .Select(p => p.ParameterType)
                                        .ToArray();

                Assert.IsTrue(UnitTestHelpers.CompareMethodCallArguments(parameterTypes, visitor.MethodCallArguments, result.Arguments, mockObject.EqualityComparerFactory));
            }

            mockObject.RecordedMethodCalls.Remove(result);

            return(result);
        }
Ejemplo n.º 3
0
        private void RecordMethodCall(Func <MethodInfo, MethodInfo> callback, params object[] arguments)
        {
            if (arguments == null)
            {
                throw new ArgumentNullException("arguments");
            }

            StackFrame frame = new StackFrame(2);

            MethodInfo method = (MethodInfo)frame.GetMethod();

            method = callback(method);

            Assert.AreEqual(method.GetParameters().Length, arguments.Length);

            RecordedMethodCall recordedMethodCall = new RecordedMethodCall(method, arguments);

            this.recordedMethodCalls.Add(recordedMethodCall);

            if (this.MethodCallRecorded != null)
            {
                this.MethodCallRecorded(this, recordedMethodCall);
            }
        }