예제 #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);
        }