1
我仍然在.NET 4.0中,我想知道这种模式是否将保持良好的各种异步单元测试情况:MsTest与Task.ContinueWith()和Task.Wait()...?
/// <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()
模式在现实世界中托起?我是比较新的正式思考单元测试(特别是异步单元测试),所以请不要憋在基础:)
完整的源代码工作的任何最终的解决方案?也许更复杂的示例用于在每个线程中通知错误,WaitAll显示摘要后? – Kiquenet 2014-01-01 10:56:20
@Kiquenet所有你要求的东西应该是开箱即用的;我认为MSTest中的异步支持不在那里,所以XUnit可能是另一种选择[http://sravi-kiran.blogspot.com/2012/11/UnitTestingAsynchronousMethodsUsingMSTestAndXUnit.html] – rasx 2014-01-03 00:03:05