Silverlight 单元测试框架有一个 [Asynchronous] 属性 (AsynchronousAttribute),它会导致测试仅在 EnqueueTestComplete() 被调用时结束。这允许一种简单的方法来编写需要在事件结束之前等待事件发生的测试。现在我试图从似乎是最流行的选择中挑选一个最喜欢的通用单元测试框架——VSUTF、NUnit、xUnit.NET、MbUnit,我想知道——你将如何使用这些框架进行异步测试?
我想我可以推出一些自定义代码来执行 Monitor.Wait 或 ResetEventWaitOne 并在测试方法结束时调用它,然后在测试结束时执行 Pulse/Set,但我正在查看是否存在通用/内置解决方案。
这是如何在 SUT 中完成的示例(来自http://smartypantscoding.com/a-cheat-sheet-for-unit-testing-silverlight-apps-on-windows-phone-7)。
[TestClass]
public class AsyncTests : SilverlightTest
{
[Asynchronous]
[TestMethod]
public void AsyncAppendStringTest()
{
var appendStrings = new List<string>() { "hello", "there" };
StringJoiner.AsyncAppendStringsWithDashes(appendStrings, (returnString) =>
{
Assert.IsTrue(string.Compare(returnString, "hello-there") == 0);
EnqueueTestComplete();
});
}
}