问题
I'm new to unit testing and async operations in visual studio/c#. Appreciate any help on this.
My Main class
class Foo
{
public async Task<string> GetWebAsync()
{
using (var client = new HttpClient())
{
var response = await client.GetAsync("https://hotmail.com");
return await response.Content.ReadAsStringAsync();
}
}
}
Unit Test
[TestMethod]
public void TestGet()
{
Foo foo = new Foo();
foo.GetWebAsync().ContinueWith((k) =>
{
Console.Write(k);
Assert.IsNotNull(null, "error");
});
}
回答1:
Make the Test async as well
[TestMethod]
public async Task TestGet() {
var foo = new Foo();
var result = await foo.GetWebAsync();
Assert.IsNotNull(result, "error");
Console.Write(result);
}
来源:https://stackoverflow.com/questions/42027432/unit-testing-async-task-in-csharp