我正在 Flex 应用程序中测试一些事件分派(dispatch)代码,使用 FlexUnit 的 addAsync
方法测试事件是否已分派(dispatch)。到目前为止很棒,我可以确保至少触发了一个事件。但是,我想更详细一点;我想确保准确地派发我期望的事件集。是否有有用的测试模式(或者,甚至不同的测试框架——我很灵活!)来完成这个?
我试过这段代码,但它似乎没有被第二次调用:
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);
}
最佳答案
回应评论...
What if the event is dispatched directly? responseSelected doesn't trigger an asynchronous event on a composite object, it simply dispatched the RESPONSE_CHANGED event itself directly. I'm not seeing how this approach can be mocked using your method. Mind you, I'm fuzzy on the mock testing practice as-is, so I'm probably missing a simple solution here.
..在这种情况下,您不需要使用模拟或 addAsync。像这样的事情会做:
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);
}
关于apache-flex - 柔性,柔性单元 : How to test that an event is dispatched twice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1167549/