I need to make sure a task is finished before moving on with the rest of my unit test.
But it is a task that is awaited inside another method (I am unit testing the method.)
I have tried awaiting the task or calling task.Wait() but each time the test does not work.
The actual method looks like this:
private async void DoStuff(long idToLookUp)
{
IOrder order = await orderService.LookUpIdAsync(idToLookUp);
OtherStuff = false;
}
I am trying to unit test it like this:
[TestMethod]
public void TestDoStuff()
{
//+ Arrange
var lookupTask = Task<IOrderableTest>.Factory.StartNew(() => validOrder);
orderService.LookUpIdAsync(Arg.Any<long>()).Returns(lookupTask);
//+ Act
myViewModel.DoStuff();
await lookupTask;
//+ Assert
myViewModel.OtherStuff.Should().BeFalse();
}
The way it fails is very unhelpful. In It causes another test to fail with "The agent process was stopped while the test was running." (From what I can read, that means a background thread threw an exception while the test was running.)
So I am wondering how I can get my code to wait for that unit test to finish.
I saw this answer that seems fairly good. But it is more geared toward the calling of the method. I am not calling it, I just have the task only.
NOTE: I am targeting .NET 4.0 using the ansyc pack.