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);
}
我正在尝试像这样对其进行单元测试:
[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();
}
我的断言在我完成模拟的LookUpIdAsync之前被调用。在我的正常代码中,这正是我想要的。但是对于我的单元测试,我不希望这样。
我正在从使用BackgroundWorker转换为Async/Await。使用BackgroundWorker时,这个功能是正常的,因为我可以等待BackgroundWorker完成。
但是似乎没有办法等待一个异步的void方法...
我该如何对这个方法进行单元测试?