Kjell Rilbe
Kjell Rilbe

Reputation: 1499

MSTest ThrowsException with conditions on the exception object?

I'm using MSTest Assert.ThrowsException, but sometimes I want to add test conditions on the thrown exception object, e.g. checking that the message mentions a specific phrase, identifier, data value or whatever.

The best solution I've found is a construct that doesn't use Assert.ThrowsException, but resorts to try...catch:

bool success = false;
try
{
    // Code to be tested goes here...
    success = true;
}
catch (Exception e)
{
    Assert.IsInstanceOfType(e, typeof(DesiredExceptionClass));
    // Tests on e go here...
}
if (success)
    Assert.Fail($"Should have thrown {nameof(DesiredExceptionClass)}.");

Is there a better way?

With "better" i mean more readable, compacter code, with less "boilerplate".

Upvotes: 1

Views: 123

Answers (2)

Old Fox
Old Fox

Reputation: 8725

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.

Upvotes: 3

Dilshod K
Dilshod K

Reputation: 3032

I'm using this way:

    Exception exception = null;
        try
        {
            // Code to be tested goes here...
        }
        catch (Exception e)
        {
            exception = e;
        }
        Assert.IsInstanceOfType(exception, typeof(DesiredExceptionClass));

Upvotes: 2

Related Questions