Created
October 4, 2012 06:47
-
-
Save Immerseit/3831866 to your computer and use it in GitHub Desktop.
Throw Exception Helper for Asserts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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