/// <summary> /// Verifies that a block of code does not throw any exceptions. /// </summary> /// <param name="testCode">A delegate to the code to be tested</param> public static void DoesNotThrow(ThrowsDelegate testCode) { Exception ex = Record.Exception(testCode); if (ex != null) { throw new DoesNotThrowException(ex); } }
/// <summary> /// Records any exception which is thrown by the given code. /// </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(ThrowsDelegate code) { try { code(); return null; } catch (Exception ex) { return ex; } }
public static Exception Throws(Type exceptionType, ThrowsDelegate 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); }
/// <summary> /// Verifies that the exact exception is thrown (and not a derived exception type). /// </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, ThrowsDelegate testCode) where T : Exception { return((T)Throws(typeof(T), testCode)); }
/// <summary> /// Verifies that the exact exception is thrown (and not a derived exception type). /// </summary> /// <typeparam name="T">The type of the exception expected to be thrown</typeparam> /// <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>(ThrowsDelegate testCode) where T : Exception { return((T)Throws(typeof(T), testCode)); }