簡體   English   中英

單元測試csharp中的異步任務

[英]unit testing async task in csharp

我是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");
    });
}

也使測試異步

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM