ExceptionAssert - a useful class for MSTest that works like Assert.Throws in NUnit


/ Published in: C#
Save to your folder(s)

Are you using MSTest as a unit testing framework? Do you miss the Assert.Throws method in NUnit? Never fear! You can use this class - ExceptionAssert - like Assert.Throws within MSTest unit tests.


Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Useful assertions for actions that are expected to throw an exception.
  3. /// </summary>
  4. public static class ExceptionAssert
  5. {
  6. /// <summary>
  7. /// Executes an exception, expecting an exception to be thrown.
  8. /// Like Assert.Throws in NUnit.
  9. /// </summary>
  10. /// <param name="action">The action to execute</param>
  11. /// <returns>The exception thrown by the action</returns>
  12. public static Exception Throws(Action action)
  13. {
  14. return Throws(action, null);
  15. }
  16.  
  17. /// <summary>
  18. /// Executes an exception, expecting an exception to be thrown.
  19. /// Like Assert.Throws in NUnit.
  20. /// </summary>
  21. /// <param name="action">The action to execute</param>
  22. /// <param name="message">The error message if the expected exception is not thrown</param>
  23. /// <returns>The exception thrown by the action</returns>
  24. public static Exception Throws(Action action, string message)
  25. {
  26. try
  27. {
  28. action();
  29. }
  30. catch (Exception ex)
  31. {
  32. // The action method has thrown the expected exception.
  33. // Return the exception, in case the unit test wants to perform further assertions on it.
  34. return ex;
  35. }
  36.  
  37. // If we end up here, the expected exception was not thrown. Fail!
  38. throw new AssertFailedException(message ?? "Expected exception was not thrown.");
  39. }
  40.  
  41. /// <summary>
  42. /// Executes an exception, expecting an exception of a specific type to be thrown.
  43. /// Like Assert.Throws in NUnit.
  44. /// </summary>
  45. /// <param name="action">The action to execute</param>
  46. /// <returns>The exception thrown by the action</returns>
  47. public static T Throws<T>(Action action) where T : Exception
  48. {
  49. return Throws<T>(action, null);
  50. }
  51.  
  52. /// <summary>
  53. /// Executes an exception, expecting an exception of a specific type to be thrown.
  54. /// Like Assert.Throws in NUnit.
  55. /// </summary>
  56. /// <param name="action">The action to execute</param>
  57. /// <param name="message">The error message if the expected exception is not thrown</param>
  58. /// <returns>The exception thrown by the action</returns>
  59. public static T Throws<T>(Action action, string message) where T : Exception
  60. {
  61. try
  62. {
  63. action();
  64. }
  65. catch (Exception ex)
  66. {
  67. T actual = ex as T;
  68. if (actual == null)
  69. {
  70. throw new AssertFailedException(message ?? String.Format("Expected exception of type {0} not thrown. Actual exception type was {1}.", typeof(T), ex.GetType()));
  71. }
  72.  
  73. // The action method has thrown the expected exception of type 'T'.
  74. // Return the exception, in case the unit test wants to perform further assertions on it.
  75. return actual;
  76. }
  77.  
  78. // If we end up here, the expected exception of type 'T' was not thrown. Fail!
  79. throw new AssertFailedException(message ?? String.Format("Expected exception of type {0} not thrown.", typeof(T)));
  80. }
  81. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.