xunit Assert.ThrowsAsync() does not work properly?

2019-04-03 05:31发布

So I have a test like the following:

    [Fact]
    public void Test1()
    {
        Assert.ThrowsAsync<ArgumentNullException>(() => MethodThatThrows());
    }

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

To my surprise, Test1 passes successfully. To make it fail I have to write like this:

Assert.Throws<ArgumentNullException>(() => MethodThatThrows().Wait());

What is the purpose of ThrowsAsync(), if it does not work in the scenario above?

1条回答
神经病院院长
2楼-- · 2019-04-03 06:06

You're supposed to await the result (see xunit's acceptance tests).

[Fact] public async Task Test1()
{
    await Assert.ThrowsAsync<ArgumentNullException>(() => MethodThatThrows());
}

In this specific degenerate case, you could just return the Task that Assert.ThrowsAsync yields without using await, but the key thing is you need to hand the resulting Task back to the xUnit framework, i.e.

[Fact]
public Task Test1() =>
    Assert.ThrowsAsync<ArgumentNullException>(MethodThatThrows);
查看更多
登录 后发表回答