Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xUnit Assert.All() async

I have this example test using xUnit:

    [Fact]
    public void SomeTest()
    {
        Assert.All(itemList, async item=>
                {
                    var i = await Something(item);
                    Assert.Equal(item,i);
                });
    }

Is there a good solution to make the whole test async/awaitable?

like image 386
J2ghz Avatar asked Jul 26 '16 17:07

J2ghz


1 Answers

There's no built-in async All. You can use Task.WhenAll:

[Fact]
public async Task SomeTest()
{
    var itemList = ...;
    var results = await Task.WhenAll(itemList.Select(async item =>
    {
        var i = await Something(item);
        return i;
    }));
    Assert.All(results, result => Assert.Equal(1, result));
}
like image 120
Eli Arbel Avatar answered Sep 20 '22 08:09

Eli Arbel