Search code examples
c#asynchronousasync-awaitnsubstitute

Check calls Received() for async method


When I run the following code:

[Test]
public async Task Can_Test_Update()
{
    var response = await _controller.UpdateAsync(Guid.NewGuid());
    response.Valid.Should().BeTrue();

    _commands.Received().UpdateAsync(
        Arg.Is<Something>(
            l => l.Status == Status.Updated)); 
}

If I add "await" preceding the "_commands.Received().UpdateAsync", it throws a null reference exception. How can I stop this happening, or is await not necessary?


Solution

  • I found an answer here.

    Received.InOrder(async () =>
    {
        await _Commands.UpdateAsync(Arg.Is<Lobby>(l => l.Status == Status.Updated));
    });