Created
January 26, 2014 20:47
-
-
Save gilles-leblanc/8639247 to your computer and use it in GitHub Desktop.
A better alternative than using the ExpectedException attribute for catching exceptions in C#.
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
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TestUtilities | |
{ | |
public static class AssertExtension | |
{ | |
public static T Throws<T>(Action expressionUnderTest, string exceptionMessage = "Expected exception has not been thrown by target of invocation.") where T : Exception | |
{ | |
try | |
{ | |
expressionUnderTest(); | |
} | |
catch (T exception) | |
{ | |
return exception; | |
} | |
Assert.Fail(exceptionMessage); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment