Exemple #1
0
        private object RunAsyncTestMethod(TestExecutionContext context)
        {
            object result = Reflect.InvokeMethod(testMethod.Method.MethodInfo, context.TestObject, arguments);

            try
            {
                return(AwaitUtils.Await(testMethod.Method.MethodInfo, result));
            }
            catch (Exception e)
            {
                throw new NUnitException("Rethrown", e);
            }
        }
Exemple #2
0
        /// <summary>
        /// Verifies that an async delegate throws a particular exception when called.
        /// </summary>
        /// <param name="expression">A constraint to be satisfied by the exception</param>
        /// <param name="code">A TestSnippet delegate</param>
        /// <param name="message">The message that will be displayed on failure</param>
        /// <param name="args">Arguments to be used in formatting the message</param>
        public static Exception ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, string message, params object[] args)
        {
            Exception caughtException = null;

            try
            {
                AwaitUtils.Await(code, code.Invoke());
            }
            catch (Exception e)
            {
                caughtException = e;
            }

            Assert.That(caughtException, expression, message, args);

            return(caughtException);
        }
        /// <summary>
        /// Executes the code and returns success if an exception is thrown.
        /// </summary>
        /// <param name="actual">A delegate representing the code to be tested</param>
        /// <returns>True if an exception is thrown, otherwise false</returns>
        public override ConstraintResult ApplyTo <TActual>(TActual actual)
        {
            TestDelegate code            = actual as TestDelegate;
            Exception    caughtException = null;

            if (code != null)
            {
#if ASYNC
                if (AwaitUtils.IsAsyncVoid(code))
                {
                    throw new ArgumentException("'async void' methods are not supported. Please use 'async Task' instead.");
                }
#endif
                try
                {
                    code();
                }
                catch (Exception ex)
                {
                    caughtException = ex;
                }
            }
#if ASYNC
            AsyncTestDelegate asyncCode = actual as AsyncTestDelegate;
            if (asyncCode != null)
            {
                try
                {
                    AwaitUtils.Await(asyncCode, asyncCode.Invoke());
                }
                catch (Exception ex)
                {
                    caughtException = ex;
                }
            }
            if (code == null && asyncCode == null)
#else
            else
#endif
            {
                throw new ArgumentException(string.Format("The actual value must be a TestDelegate or AsyncTestDelegate but was {0}", actual.GetType().Name), "actual");
            }
            return(new ThrowsExceptionConstraintResult(this, caughtException));
        }
Exemple #4
0
            internal static Exception Intercept(object invocation)
            {
                var invocationDescriptor = GetInvocationDescriptor(invocation);

#if ASYNC
                if (AwaitUtils.IsAwaitable(invocationDescriptor.Delegate))
                {
                    try
                    {
                        object result = invocationDescriptor.Invoke();
                        AwaitUtils.Await(invocationDescriptor.Delegate, result);
                        return(null);
                    }
                    catch (Exception ex)
                    {
                        return(ex);
                    }
                }

                if (AwaitUtils.IsAsyncVoid(invocationDescriptor.Delegate))
                {
                    throw new ArgumentException("'async void' methods are not supported. Please use 'async Task' instead.");
                }
#endif
                using (new TestExecutionContext.IsolatedContext())
                {
                    try
                    {
                        invocationDescriptor.Invoke();
                        return(null);
                    }
                    catch (Exception ex)
                    {
                        return(ex);
                    }
                }
            }