带有Task.ContineWith()和Task.Wet()的MsTest.

本文关键字:Task MsTest Wet 带有 ContineWith | 更新日期: 2023-09-27 18:27:22

我还在.NET 4.0中,我想知道这种模式是否能在各种异步单元测试情况下保持良好:

/// <summary>
/// Noaa weather test: should read remote XML.
/// </summary>
[TestMethod]
public void ShouldReadRemoteXml()
{
    var uri = new Uri("http://www.weather.gov/xml/current_obs/KLAX.xml");
    var wait = HttpVerbAsyncUtility.GetAsync(uri)
        .ContinueWith(
        t =>
        {
            Assert.IsFalse(t.IsFaulted, "An unexpected XML read error has occurred.");
            Assert.IsTrue(t.IsCompleted, "The expected XML read did not take place.");
            if(t.IsCompleted && !t.IsFaulted)
                FrameworkFileUtility.Write(t.Result, @"NoaaWeather.xml");
        })
        .Wait(TimeSpan.FromSeconds(7));
    Assert.IsTrue(wait, "The expected XML read did not take place within seven seconds.");
}

这种Task.ContinueWith()...Task.Wait()模式在现实世界中会成立吗?我对正式考虑单元测试(尤其是异步单元测试)还比较陌生,所以请不要拘泥于基础:)

带有Task.ContineWith()和Task.Wet()的MsTest.

单元测试框架通常不能很好地使用异步代码,这是我们在C#和Visual Basic中添加await时非常关心的问题。

通常情况下,测试方法会命中第一个"等待",然后立即返回。然后,测试被标记为已成功,因为它返回时没有错误,即使在继续中会产生错误。

我们正在与单元测试框架提供商合作,为即将发布的Visual Studio版本改进这个故事MSTest和XUnit.NET现在可以在VS11Beta中正确处理异步方法我的建议是:掌握VS 11测试版,并尝试其中的异步单元测试支持。如果你不喜欢,现在将是在异步论坛上给出反馈的好时机