Home > Net >  XUnit ThrowsAsync<> doesn't seem to be checking the exception type. Is this a bug?
XUnit ThrowsAsync<> doesn't seem to be checking the exception type. Is this a bug?

Time:10-21

XUnit ThrowsAsync<> doesn't seem to be checking the exception type.

I am trying to check the type of the exception thrown in my unit test. However, XUnit does not seem to be not checking the exception type but only that its an Exception.

Surely the below test should fail as the exception thrown is of type NotImplementedException but the test was expecting ArgumentException. It passes as long as any kind of exception is thrown.

public class AssertThrowsTests
{
    [Fact]
    public void Tests()
    {
        // Using a method as a delegate
        Assert.ThrowsAsync<ArgumentException>(async () => await MethodThatThrows());
    }

    async Task MethodThatThrows()
    {
        await Task.Delay(100);
        throw new NotImplementedException();
    }
}

Is this a bug with XUnit or my code?

CodePudding user response:

You're ignoring the fact that ThrowsAsync is itself asynchronous, and returns a task that you should await. Change your test to:

[Fact]
public async Task Tests()
{
    await Assert.ThrowsAsync<ArgumentException>(async () => await MethodThatThrows());
}
  • Related