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 MockDelegate getMockDelegate(StaticMock staticMock, MethodInfo method)
        {
            var mockDelegate = staticMock.GetMockDelegate(method);

            if (!mockDelegate.IsMocked)
            {
                mockDelegate.Delegate = createDelegateFor(mockDelegate.DelegateType);
            }

            return(mockDelegate);
        }
Esempio n. 3
0
        public static MockTarget Setup(this StaticMock staticMock, Expression <Action> target)
        {
            var methodCallExpression = (MethodCallExpression)target.Body;
            var method = methodCallExpression.Method;

            var mockDelegate = staticMock.GetMockDelegate(method);

            MockDelegateTarget mockDelegateTarget;

            if (!mockDelegate.IsMocked)
            {
                mockDelegateTarget    = new MockDelegateTarget(method);
                mockDelegate.Delegate = mockDelegateTarget.CreateDelegateFor(mockDelegate.DelegateType);
            }
            else
            {
                mockDelegateTarget = (MockDelegateTarget)mockDelegate.Delegate.Target;
            }

            return(new MockTarget(mockDelegateTarget, methodCallExpression));
        }