我是单元测试的新手,并尝试为使用任务的 WPF ViewModel 编写单元测试。我在 VM 中有一个方法连接到 WPF 中的按钮。下面的代码总结了我正在尝试做的事情。类 MainPageViewModel { 私有 IService 服务_;
public void StartTask()
{
var task = service_.StartServiceAsync();
task.ContinueWith(AfterService);
}
private void AfterService(Task<IResult> result)
{
//update UI with result
}
}
class TestClass
{
[TestMethod]
public Test_StartTask()
{
MainPageViewModel vm = new MainPageViewModel();
vm.StartTask();
//need to check if UI is updated but since the AfterService is called on a different thread the assert fails
}
}
在我的测试方法中,我无法在 StartTask() 调用后编写 Assert,请帮助我了解如何处理此类情况?TIA。