7

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

2 回答 2

1

Visual Studio 现在支持具有async Task签名的测试,并且测试在异步方法完成时完成。

于 2013-06-06T21:37:18.930 回答
1

受 Caliburn Micro 启发,Christopher Bennage 有一个有趣的方法:

http://devlicio.us/blogs/christopher_bennage/archive/2011/01/17/improving-asynchronous-tests-for-silverlight.aspx

于 2012-01-06T16:06:42.027 回答