/// <summary> /// Assertion method to verify that an exception is thrown. /// </summary> /// <param name="task">The function or method under test.</param> /// <param name="expectedMessage">The expected message.</param> /// <param name="messageOptions">The message options for specifying assertion rules for the exception message.</param> /// <param name="inheritOptions">The inherit options for specifying assertion rules for the exception type.</param> /// <typeparam name="T">The type of the expected exception.</typeparam> /// <returns>The <see cref="T"/>. Returns the exception instance.</returns> public static T Throws <T>(Action task, string expectedMessage, ExceptionMessageCompareOptions messageOptions, ExceptionInheritanceOptions inheritOptions) where T : Exception { try { task(); } catch (Exception ex) { return(ExceptionAssert.CheckException <T>(expectedMessage, messageOptions, inheritOptions, ex)); } ExceptionAssert.OnNoExceptionThrown <T>(); return(default(T)); }
/// <summary> /// This checks if an async method throws an exception as InnerException (chained exception are ignored) /// </summary> /// <param name="task">The async task under test.</param> /// <param name="expectedMessage">The expected message.</param> /// <param name="messageOptions">The message options for specifying assertion rules for the exception message.</param> /// <param name="inheritOptions">The inherit options for specifying assertion rules for the exception type.</param> /// <typeparam name="T">The type of the expected exception.</typeparam> /// <returns>The <see cref="T"/>. Returns the exception instance.</returns> public static T ThrowsAsync <T>(Task task, string expectedMessage, ExceptionMessageCompareOptions messageOptions, ExceptionInheritanceOptions inheritOptions) where T : Exception { try { task.Wait(); } catch (AggregateException aggregateEx) { return(ExceptionAssert.CheckException <T>(expectedMessage, messageOptions, inheritOptions, aggregateEx.InnerException)); } ExceptionAssert.OnNoExceptionThrown <T>(); return(default(T)); }
public static void Throws <T>(Action task, string expectedMessage, ExceptionMessageCompareOptions options) where T : Exception { try { task(); } catch (Exception ex) { AssertExceptionType <T>(ex); AssertExceptionMessage(ex, expectedMessage, options); return; } if (typeof(T).Equals(new Exception().GetType())) { Assert.Fail("Expected exception but no exception was thrown."); } else { Assert.Fail(string.Format("Expected exception of type {0} but no exception was thrown.", typeof(T))); } }
public static T Throws <T>(Action task, string expectedMessage, ExceptionMessageCompareOptions messageOptions, ExceptionInheritanceOptions inheritOptions) where T : Exception { try { task(); } catch (Exception ex) { AssertExceptionType <T>(ex, inheritOptions); AssertExceptionMessage(ex, expectedMessage, messageOptions); return((T)ex); } if (typeof(T).Equals(new Exception().GetType())) { Assert.Fail("Expected exception but no exception was thrown."); } else { Assert.Fail(string.Format("Expected exception of type {0} but no exception was thrown.", typeof(T))); } return(default(T)); }
private static void AssertExceptionMessage(Exception ex, string expectedMessage, ExceptionMessageCompareOptions options) { if (!string.IsNullOrEmpty(expectedMessage)) { switch (options) { case ExceptionMessageCompareOptions.Exact: Assert.AreEqual(ex.Message.ToUpper(), expectedMessage.ToUpper(), "Expected exception message failed."); break; case ExceptionMessageCompareOptions.Contains: Assert.IsTrue(ex.Message.Contains(expectedMessage), string.Format("Expected exception message does not contain <{0}>.", expectedMessage)); break; default: throw new ArgumentOutOfRangeException("options"); } } }
public static void Throws(Action task, string expectedMessage, ExceptionMessageCompareOptions options) { Throws <Exception>(task, expectedMessage, options); }
public static void Throws <T>(this IAssertion assertion, Action task, string expectedMessage, ExceptionMessageCompareOptions options) where T : Exception { Throws <T>(task, expectedMessage, options); }
/// <summary> /// The check exception. /// </summary> /// <param name="expectedMessage">The expected message.</param> /// <param name="messageOptions">The message options for specifying assertion rules for the exception message.</param> /// <param name="inheritOptions">The inherit options for specifying assertion rules for the exception type.</param> /// <param name="ex">The exception thrown by the method under test.</param> /// <typeparam name="T">The type of the expected exception.</typeparam> /// <returns>The <see cref="T"/>. Returns the exception instance.</returns> public static T CheckException <T>(string expectedMessage, ExceptionMessageCompareOptions messageOptions, ExceptionInheritanceOptions inheritOptions, Exception ex) where T : Exception { AssertExceptionType <T>(ex, inheritOptions); AssertExceptionMessage(ex, expectedMessage, messageOptions); return((T)ex); }
public static Exception Throws(Action task, string expectedMessage, ExceptionMessageCompareOptions options, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) { return(Throws <Exception>(task, expectedMessage, options, inheritOptions)); }
public static T Throws <T>(this IAssertion assertion, Action task, string expectedMessage, ExceptionMessageCompareOptions options, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) where T : Exception { return(Throws <T>(task, expectedMessage, options, inheritOptions)); }
public static Exception ThrowsAsync(this IAssertion assertion, Task task, string expectedMessage, ExceptionMessageCompareOptions options, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) { return(ThrowsAsync <Exception>(task, expectedMessage, options, inheritOptions)); }