Skip to content

Instantly share code, notes, and snippets.

@Immerseit
Created October 4, 2012 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Immerseit/3831866 to your computer and use it in GitHub Desktop.
Save Immerseit/3831866 to your computer and use it in GitHub Desktop.
Throw Exception Helper for Asserts
// var myTest = new Namespace.TheService();
// var ex = ExceptionFactory.Throws<InvalidOperationException>(() =>
// myTest.Result = myTest.Execute() );
// Assert.AreEqual(myTest.Result, null);
// Assert.AreEqual(ex.AnyProp, "ExpectedData");
public static class ExceptionFactory
{
public static T Throws<T>(Action action) where T : Exception
{
try
{
action();
}
catch (T ex)
{
return ex;
}
Assert.Fail("Exception of type {0} was thrown.", typeof(T));
// Don't show the exception message
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment