单元测试csharp中的异步任务

use*_*195 2 .net c# unit-testing async-await

我是visual studio/c#中的单元测试和异步操作的新手.感谢任何帮助.

我的主要课程

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

单元测试

[TestMethod]
public void TestGet()
{
    Foo foo = new Foo();

    foo.GetWebAsync().ContinueWith((k) =>
    {
        Console.Write(k);
        Assert.IsNotNull(null, "error");
    });
}
Run Code Online (Sandbox Code Playgroud)

Nko*_*osi 8

也使测试异步

[TestMethod]
public async Task TestGet() {
    var foo = new Foo();
    var result = await foo.GetWebAsync();
    Assert.IsNotNull(result, "error");
    Console.Write(result);
}
Run Code Online (Sandbox Code Playgroud)