问题描述
我正在 Flex 应用程序中测试一些事件调度代码,使用 FlexUnit 的 addAsync
方法来测试事件的调度.到目前为止很好,我可以确保至少触发了一个事件.但是,我想说得更详细一些;我想确保准确地调度了我期望的事件集.是否有一个有用的测试模式(或者,甚至是不同的测试框架——我很灵活!)来完成这个?
I'm testing some event dispatch code in a Flex app, using FlexUnit's addAsync
method for testing that events are dispatched. Great so far, I can ensure that at least one event was fired. However, I want to be a bit more detailed; I want to ensure that exactly the set of events I'm expecting are dispatched. Is there a useful test pattern (or, even, different test framework -- I'm flexible!) to accomplish this?
我试过这段代码,但它似乎没有第二次被调用:
I tried this code, but it doesn't seem to get invoked the second time:
protected function expectResultPropertyChange(event: Event, numberOfEvents: int = 1): void {
trace("Got event " + event + " on " + event.target + " with " + numberOfEvents + " traces left...");
assertTrue(event.type == ResponseChangedEvent.RESPONSE_CHANGED);
if (numberOfEvents > 1) {
event.target.addEventListener(ResponseChangedEvent.RESPONSE_CHANGED, addAsync(expectResultPropertyChange, 1000, numberOfEvents - 1));
}
}
public function testSomething(): void {
requiredQuestion.addEventListener(ResponseChangedEvent.RESPONSE_CHANGED, addAsync(expectResultPropertyChange, 1000, 2));
requiredQuestion.responseSelected("1", true);
requiredQuestion.responseSelected("2", true);
}
推荐答案
回应评论...
如果事件被分派怎么办直接地?responseSelected 没有在 a 上触发异步事件复合对象,它只是简单地调度RESPONSE_CHANGED 事件本身直接地.我不明白这是怎么回事可以使用您的方法来模拟方法.请注意,我对模拟测试实践,所以我可能缺少一个简单的解决方案在这里.
...在这种情况下,您不需要使用模拟或 addAsync.像这样的事情会做:
..in that case you don't need to use a mock or addAsync. Something like this will do:
public function testSomething(): void
{
var requiredQuestion : RequiredQuestion = new RequiredQuestion();
var callCount : int = 0;
requiredQuestion.addEventListener(ResponseChangedEvent.RESPONSE_CHANGED, function(event : ResponseChangedEvent)
{
callCount++;
});
requiredQuestion.responseSelected("1", true);
requiredQuestion.responseSelected("2", true);
assertEquals(2, callCount);
}
这篇关于Flex、Flexunit:如何测试一个事件是否被分派了两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!