Czekaj na wywołanie metody Async Void dla testów jednostkowych
Mam metodę, która wygląda tak:
private async void DoStuff(long idToLookUp)
{
IOrder order = await orderService.LookUpIdAsync(idToLookUp);
// Close the search
IsSearchShowing = false;
}
//Other stuff in case you want to see it
public DelegateCommand<long> DoLookupCommand{ get; set; }
ViewModel()
{
DoLookupCommand= new DelegateCommand<long>(DoStuff);
}
Próbuję przetestować to tak:
[TestMethod]
public void TestDoStuff()
{
//+ Arrange
myViewModel.IsSearchShowing = true;
// container is my Unity container and it setup in the init method.
container.Resolve<IOrderService>().Returns(orderService);
orderService = Substitute.For<IOrderService>();
orderService.LookUpIdAsync(Arg.Any<long>())
.Returns(new Task<IOrder>(() => null));
//+ Act
myViewModel.DoLookupCommand.Execute(0);
//+ Assert
myViewModel.IsSearchShowing.Should().BeFalse();
}
Moje assert jest wywoływane zanim zrobię z wyszydzonym LookUpIdAsync. W moim normalnym kodzie jest to, czego chcę. Ale dla mojego testu jednostkowego tego nie chcę.
Konwersję do Async / Await z używania BackgroundWorker. W tle pracownik działał poprawnie, ponieważ mogłem poczekać na zakończenie działania BackgroundWorker.
Ale nie ma sposobu na oczekiwanie na asynchroniczną metodę pustki ...
Jak mogę przetestować tę metodę?