Async methods return a Task that needs to be awaited to get results. If it is not awaited, the method will return immediately, without waiting for the async task to finish. Consider the following method, which we're using to write a unit test case with xUnit:
private async Task<int> SomeFunction()
{
int result =await Task.Run(() =>
{
Thread.Sleep(1000);
return 5;
});
return result;
}
The method returns a constant value of 5 after a delay of 1 second. Since the method used Task, we made use of the async and await keywords to get the expected results. The following is a very simple test case we can use to test this method using MSTest:
[TestMethod]
public async void SomeFunctionShouldFailAsExpectedValueShouldBe5AndNot3()
{
var result = await SomeFunction();
...