/// <summary> /// Asserts the type of the exception with the expected type (T) /// </summary> /// <param name="ex">The exception thrown by the method under test.</param> /// <param name="inheritanceOptions">The options.</param> /// <typeparam name="T">The type of the expected exception.</typeparam> /// <exception cref="ArgumentOutOfRangeException">Throws exception for invalid or None option.</exception> private static void AssertExceptionType <T>(Exception ex, ExceptionInheritanceOptions inheritanceOptions) { Assert.IsType <T>(ex); ////Assert.IsInstanceOfType(ex, typeof(T), "Expected exception type failed."); //switch (inheritanceOptions) //{ // case ExceptionInheritanceOptions.Exact: // AssertExceptionNotInherited<T>(ex); // case ExceptionInheritanceOptions.Inherits: // break; // default: // throw new ArgumentOutOfRangeException("inheritanceOptions"); //} }
private static void AssertExceptionType <T>(Exception ex, ExceptionInheritanceOptions options) { Assert.IsInstanceOfType(ex, typeof(T), "Expected exception type failed."); switch (options) { case ExceptionInheritanceOptions.Exact: AssertExceptionNotInherited <T>(ex); break; case ExceptionInheritanceOptions.Inherits: break; default: throw new ArgumentOutOfRangeException("options"); } }
public static void Throws <T>(Action task, string expectedMessage, ExceptionInheritanceOptions optionsInheritance, ExceptionMessageCompareOptions optionsMessage) where T : Exception { try { task(); } catch (Exception ex) { AssertExceptionType <T>(ex, optionsInheritance); AssertExceptionMessage(ex, expectedMessage, optionsMessage); 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 Exception Throws(Action task, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) { return(Throws <Exception>(task, null, ExceptionMessageCompareOptions.None, inheritOptions)); }
public static Exception Throws(Action task, string expectedMessage, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) { return(Throws <Exception>(task, expectedMessage, ExceptionMessageCompareOptions.Exact, inheritOptions)); }
public static Exception Throws(this IAssertion assertion, 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, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) where T : Exception { return(Throws <T>(task, expectedMessage, ExceptionMessageCompareOptions.Exact, inheritOptions)); }
public static T Throws <T>(Action task, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) where T : Exception { return(Throws <T>(task, null, ExceptionMessageCompareOptions.None, inheritOptions)); }
public static T ThrowsAsync <T>(Task task, string expectedMessage, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) where T : Exception { return(ThrowsAsync <T>(task, expectedMessage, ExceptionMessageCompareOptions.Exact, inheritOptions)); }
/// <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)); }
public static Exception ThrowsAsync(this IAssertion assertion, Task task, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) { return(ThrowsAsync <Exception>(task, null, ExceptionMessageCompareOptions.None, inheritOptions)); }
public static Exception ThrowsAsync(this IAssertion assertion, Task task, string expectedMessage, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) { return(ThrowsAsync <Exception>(task, expectedMessage, ExceptionMessageCompareOptions.Exact, inheritOptions)); }
public static Exception ThrowsAsync(Task task, string expectedMessage, ExceptionMessageCompareOptions options, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) { return(ThrowsAsync <Exception>(task, expectedMessage, options, inheritOptions)); }
public static T ThrowsAsync <T>(this IAssertion assertion, Task task, string expectedMessage, ExceptionMessageCompareOptions options, ExceptionInheritanceOptions inheritOptions = ExceptionInheritanceOptions.Inherits) where T : Exception { return(ThrowsAsync <T>(task, expectedMessage, options, inheritOptions)); }
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)); }
/// <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); }
/// <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)); }