Пример #1
0
        /// <summary>
        /// Specify the return value of the arranged mock object's method call.
        /// </summary>
        /// <param name="result"> The value to be returned. </param>
        /// <param name="onlyIfParametersMatch">
        /// True if the return value should only be returned if the arranged method's input parameters match,
        /// false otherwise.
        /// </param>
        /// <returns>
        /// An <see cref="ExecutorWithMocks{T}"/> that can be used to arrange mock objects and/or execute a method
        /// (to be tested) on an instance of type <typeparamref name="T"/>.
        /// </returns>
        public ExecutorWithMocks <T> Returns(TResult result, bool onlyIfParametersMatch = false)
        {
            var expression = ArrangeAsync;

            if (onlyIfParametersMatch == false)
            {
                var visitor = new IgnoreMockParametersVisitor();
                expression = visitor.VisitAndConvert(ArrangeAsync, nameof(Returns));
            }

            var           mockType    = typeof(Mock <TMock>);
            Action <Mock> arrangement = (mock) => ((Mock <TMock>)mock).Setup(expression).Returns(Task.FromResult(result));

            return(CreateExecutor(mockType, arrangement));
        }
Пример #2
0
        /// <summary>
        /// Specify an exception that is thrown when the arranged mock object's method is called.
        /// </summary>
        /// <typeparam name="TException"> The type of the exception to be thrown. </typeparam>
        /// <param name="onlyIfParametersMatch">
        /// True if the exception should only be thrown if the arranged method's input parameters match,
        /// false otherwise.
        /// </param>
        /// <returns>
        /// An <see cref="ExecutorWithMocks{T}"/> that can be used to arrange mock objects and/or execute a method
        /// (to be tested) on an instance of type <typeparamref name="T"/>.
        /// </returns>
        public ExecutorWithMocks <T> Throws <TException>(bool onlyIfParametersMatch = false)
            where TException : Exception, new()
        {
            var expression = ArrangeAsync;

            if (onlyIfParametersMatch == false)
            {
                var visitor = new IgnoreMockParametersVisitor();
                expression = visitor.VisitAndConvert(ArrangeAsync, nameof(Returns));
            }

            var           mockType    = typeof(Mock <TMock>);
            Action <Mock> arrangement = (mock) => ((Mock <TMock>)mock).Setup(expression).Throws <TException>();

            return(CreateExecutor(mockType, arrangement));
        }