Example #1
0
        /// <summary>
        /// Wraps code containing a series of assertions, which should all
        /// be executed, even if they fail. Failed results are saved and
        /// reported at the end of the code block.
        /// </summary>
        /// <param name="testDelegate">A TestDelegate to be executed in Multiple Assertion mode.</param>
        public static void Multiple(AsyncTestDelegate testDelegate)
        {
            TestExecutionContext context = TestExecutionContext.CurrentContext;

            Guard.OperationValid(context != null, "Assert.Multiple called outside of a valid TestExecutionContext");

            context.MultipleAssertLevel++;

            try
            {
                using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(testDelegate))
                {
                    var result = testDelegate();
                    region.WaitForPendingOperationsToComplete(result);
                }
            }
            finally
            {
                context.MultipleAssertLevel--;
            }

            if (context.MultipleAssertLevel == 0 && context.CurrentResult.PendingFailures > 0)
            {
                context.CurrentResult.RecordTestCompletion();
                throw new MultipleAssertException(context.CurrentResult);
            }
        }
Example #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;

            using (var region = AsyncInvocationRegion.Create(code))
            {
                try
                {
                    var task = code();
                    region.WaitForPendingOperationsToComplete(task);
                }
                catch (Exception e)
                {
                    caughtException = e;
                }
            }

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

            return(caughtException);
        }
Example #3
0
        /// <summary>
        /// Verifies that a 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 Throws(IResolveConstraint expression, TestDelegate code, string message, params object[] args)
        {
            Exception caughtException = null;

#if ASYNC
            if (AsyncInvocationRegion.IsAsyncOperation(code))
            {
                using (var region = AsyncInvocationRegion.Create(code))
                {
                    code();

                    try
                    {
                        region.WaitForPendingOperationsToComplete(null);
                    }
                    catch (Exception e)
                    {
                        caughtException = e;
                    }
                }
            }
            else
#endif

            using (new TestExecutionContext.IsolatedContext())
            {
                try
                {
                    code();
                }
                catch (Exception ex)
                {
                    caughtException = ex;
                }
            }

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

            return(caughtException);
        }