There is a pattern I have post in the past to verify additional expectations when an exception should be raise.
It seems like you are trying to reduce the "Boilerplate Code" so i have created the following helper class:
public static class ExceptionAssert
{
/// <summary>
/// Use this method if your UT is using <see cref="ExpectedExceptionAttribute"/> and you want to verify additional expectations
/// </summary>
/// <typeparam name="T">The expected exception type base</typeparam>
/// <param name="action">Execute the unit to test</param>
/// <param name="verifier">Verify the additional expectations</param>
public static void AssertException<T>(Action action, Action<T> verifier) where T: Exception
{
try
{
action();
}
catch(T e)
{
verifier(e);
throw;
}
}
/// <summary>
/// Use this method if your UT is not using <see cref="ExpectedExceptionAttribute"/> and you want to verify additional expectations
/// </summary>
/// <typeparam name="T">The expected exception type base</typeparam>
/// <param name="action">Execute the unit to test</param>
/// <param name="verifier">Verify the additional expectations</param>
/// <param name="allowDriven">Indicates if the raised exception can be an instance of driven class</param>
public static void AssertExceptionWithoutExcepctedExceptionAttribute<T>(Action action, Action<T> verifier, bool allowDriven = true) where T : Exception
{
try
{
action();
Assert.Fail("No Exception raised");
}
catch (T e)
{
if (!allowDriven && e.GetType() != typeof(T))
{
Assert.Fail($"The raised exception :: {e.GetType()} is a driven instance of :: {typeof(T)}");
}
verifier(e);
}
}
}
Now you can execute your UT as one of the following:
[TestMethod]
[ExpectedException(typeof(Exception), AllowDerivedTypes = true)] // change the attribute settings
public void Foo()
{
// do arrange:
ExceptionAssert.AssertException<Exception>(() => // change "Exception" to the required exception type or left it as any exception
{
// do the act:
}, exception =>
{
// do you asserts statements:
});
}
[TestMethod]
public void FooBar()
{
// do arrange:
ExceptionAssert.AssertExceptionWithoutExcepctedExceptionAttribute<Exception>(() => // change "Exception" to the required exception type or left it as any exception
{
// do the act:
}, exception =>
{
// do you asserts statements:
}, false);
}
BTW, IMO you should never verify the exception itself except his type/driven(or if it is a custom exception with additional information) since your code should never depends on his MSG, call-stack and etc... I think that this is the reason why MS didn't add to ExpectedExceptionAttribute the ability to verify the MSG.