单元测试csharp中的异步任务

时间:2017-02-03 15:16:25

标签: c# .net 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();
        }
    }
}

单元测试

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

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

1 个答案:

答案 0 :(得分:5)

使测试异步

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