简体   繁体   English

单元测试csharp中的异步任务

[英]unit testing async task in csharp

I'm new to unit testing and async operations in visual studio/c#. 我是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");
    });
}

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM