Пример #1
0
 /// <summary>
 /// Records any exception which is thrown by the given code that has
 /// a return value. Generally used for testing property accessors.
 /// </summary>
 /// <param name="code">The code which may thrown an exception.</param>
 /// <returns>Returns the exception that was thrown by the code; null, otherwise.</returns>
 public static Exception Exception(ThrowsDelegateWithReturn code)
 {
     try
     {
         code();
         return null;
     }
     catch (Exception ex)
     {
         return ex;
     }
 }
Пример #2
0
        public static Exception Throws(Type exceptionType, ThrowsDelegateWithReturn testCode)
        {
            Exception exception = Record.Exception(testCode);

            if (exception == null)
            {
                throw new ThrowsException(exceptionType);
            }

            if (!exceptionType.Equals(exception.GetType()))
            {
                throw new ThrowsException(exceptionType, exception);
            }

            return(exception);
        }
Пример #3
0
 /// <summary>
 /// Verifies that the exact exception is thrown (and not a derived exception type).
 /// Generally used to test property accessors.
 /// </summary>
 /// <typeparam name="T">The type of the exception expected to be thrown</typeparam>
 /// <param name="userMessage">The message to be shown if the test fails</param>
 /// <param name="testCode">A delegate to the code to be tested</param>
 /// <returns>The exception that was thrown, when successful</returns>
 /// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
 public static T Throws <T>(string userMessage,
                            ThrowsDelegateWithReturn testCode)
     where T : Exception
 {
     return((T)Throws(typeof(T), testCode));
 }