Skip to content

Instantly share code, notes, and snippets.

@JohnLudlow
Last active January 15, 2016 16:21
Show Gist options
  • Save JohnLudlow/83b4887be35762f7a12e to your computer and use it in GitHub Desktop.
Save JohnLudlow/83b4887be35762f7a12e to your computer and use it in GitHub Desktop.
public static class AssertEx
{
public static void Throws<T>(Action action) where T : Exception
{
Exception actual = null;
try
{
action();
}
catch (T ex)
{
actual = ex;
}
Assert.IsInstanceOfType(actual, typeof(T));
}
public static void Throws<T>(Action action, Action<T> assertion) where T : Exception
{
Exception actual = null;
try
{
action();
}
catch (T ex)
{
actual = ex;
}
Assert.IsInstanceOfType(actual, typeof(T));
assertion((T)actual);
}
public static void Throws<T>(Action action, Func<T, bool> assertion) where T : Exception
{
Exception actual = null;
try
{
action();
}
catch (T ex)
{
actual = ex;
}
Assert.IsInstanceOfType(actual, typeof(T));
Assert.IsTrue(assertion((T)actual));
}
public static T Exception<T>(Action action) where T : Exception
{
T actual = null;
try
{
action();
}
catch (T ex)
{
actual = ex;
}
return actual;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment