Await a Async Void method call for unit testing

前端 未结 8 1510
北荒
北荒 2020-12-04 17:25

I have a method that looks like this:

private async void DoStuff(long idToLookUp)
{
    IOrder order = await orderService.LookUpIdAsync(idToLookUp);   

             


        
8条回答
  •  温柔的废话
    2020-12-04 17:48

    I had a similar issue. In my case, the solution was to use Task.FromResult in the moq setup for .Returns(...) like so:

    orderService.LookUpIdAsync(Arg.Any())
        .Returns(Task.FromResult(null));
    

    Alternatively, Moq also has a ReturnsAysnc(...) method.

提交回复
热议问题