Пример #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);
        }
Пример #2
0
        public static void ReturnsForAll <T>(this StaticMock staticMock, T returnThis)
        {
            var mockDelegate = staticMock.LastMockDelegate;

            if (mockDelegate == null)
            {
                throw new StaticMockException(StaticMockException.ReturnsForAllMessage());
            }

            mockDelegate.Delegate.Target.ReturnsForAll(returnThis);
        }
Пример #3
0
        static MockDelegate getMockDelegate(StaticMock staticMock, MethodInfo method)
        {
            var mockDelegate = staticMock.GetMockDelegate(method);

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

            return(mockDelegate);
        }
Пример #4
0
        static void activate(Type type, StaticMock staticMock)
        {
            lock (staticMocks)
            {
                if (staticMocks.ContainsKey(type))
                {
                    throw new StaticMockException(StaticMockException.AlreadyActiveMessage(type));
                }

                staticMocks.Add(type, staticMock);
            }
        }
Пример #5
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()));
        }
Пример #6
0
        public static void ThrowsForAll(this StaticMock staticMock, Exception exception)
        {
            var mockDelegate = staticMock.LastMockDelegate;

            if (mockDelegate == null)
            {
                throw new StaticMockException(StaticMockException.ReturnsForAllMessage());
            }

            var returnType         = mockDelegate.Delegate.GetMethodInfo().ReturnType;
            var throwsForAllMethod = typeof(NSubstituteSupport).GetMethod(nameof(throwsForAll),
                                                                          BindingFlags.Static | BindingFlags.NonPublic);
            var genericMethod = throwsForAllMethod.MakeGenericMethod(returnType);

            genericMethod.Invoke(null, new object[] { mockDelegate.Delegate.Target, exception });
        }
Пример #7
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));
        }
Пример #8
0
        public static void For(this StaticMock staticMock, Expression <Action> target)
        {
            var methodCallExpression = (MethodCallExpression)target.Body;

            getMockDelegate(staticMock, methodCallExpression.Method);
        }