Esempio n. 1
0
        public static object Received(this StaticMock staticMock, int count, Expression <Action> target)
        {
            var methodCallExpression = (MethodCallExpression)target.Body;
            var method = methodCallExpression.Method;

            var mockDelegate = staticMock.GetMockDelegate(method);

            if (!mockDelegate.IsMocked)
            {
                throw new StaticMockException("Static methods must be substituted using `StaticMock.For` before they can be validated.");
            }

            var action = mockDelegate.Delegate.Target;

            action.Received(count);

            var argList = new List <object>();

            foreach (var arg in methodCallExpression.Arguments)
            {
                var value = ExpressionUtilities.GetValue(arg);
                argList.Add(value);
            }

            try
            {
                mockDelegate.Delegate.DynamicInvoke(argList.ToArray());
            }
            catch (TargetInvocationException e)
            {
                throw e.InnerException;
            }

            return(action);
        }
Esempio n. 2
0
            static object getKeyObject(MethodCallExpression methodCallExpression)
            {
                var argList = new List <object>();

                foreach (var arg in methodCallExpression.Arguments)
                {
                    var value = ExpressionUtilities.GetValue(arg);
                    argList.Add(value);
                }

                return(getKeyObject(methodCallExpression.Method, argList.ToArray()));
            }
Esempio n. 3
0
        public static R For <R>(this StaticMock staticMock, Expression <Func <R> > target)
        {
            var methodCallExpression = (MethodCallExpression)target.Body;
            var mockDelegate         = getMockDelegate(staticMock, methodCallExpression.Method);

            var argList = new List <object>();

            foreach (var arg in methodCallExpression.Arguments)
            {
                var value = ExpressionUtilities.GetValue(arg);
                argList.Add(value);
            }

            return((R)mockDelegate.Delegate.DynamicInvoke(argList.ToArray()));
        }