简体   繁体   中英

unit testing async task in csharp

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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