我正在使用Silverlight单元测试框架来测试一些View Manager类.某些测试需要触发PropertyChanged事件.
我目前正在使用EnqueueConditional和WaitHandles的组合
例1
[TestMethod]
[Asynchronous]
[Timeout(1000)]
public void TestNotificationExample()
{
var manager = new UserManager();
var waitHandle = new ManualResetEvent(false);
manager.PropertyChanged += (sender, propChangArgs) =>
{
waitHandler.Set();
};
manager.DoTheThingThatTriggersNotification();
// The notification event fires aynshronously to this
EnqueueConditional (() => waitHandler.WaitOne(0));
// Enqueue other tests here....
EnqueueTestComplete();
}
Run Code Online (Sandbox Code Playgroud)
这有效.但我有些问题唠叨着我:
我真的需要使用WaitHandle吗?如果我只使用一个bool,它会表现得同样如此吗?
例2
bool fHasFiredEvent = false;
manager.PropertyChanged += (sender, propChangeArgs) =>
{
fHasFiredEvent = true;
}
manager.DoTheThingThatTriggersNotification();
EnqueueConditional (() => fHasFiredEvent);
EnqueueTestComplete();
Run Code Online (Sandbox Code Playgroud)
或者如果我保留WaitHandle会更好,但会丢失TimeoutAttribute并在等待时间超时?
例3 …