Flex,Flexunit:如何测试事件发送两次? [英] Flex, Flexunit: How to test that an event is dispatched twice?
问题描述
addAsync
方法来测试事件的发送。到目前为止,我可以确保至少有一个事件被解雇。但是,我想要更详细一些;我想确保我正在发送的事件集。有没有一个有用的测试模式(或甚至不同的测试框架 - 我很灵活)来完成这个? 我尝试过这个代码, t似乎被第二次调用:
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不
触发
复合对象上的异步事件,它直接发送
RESPONSE_CHANGED事件本身
。我没有看到这个
的方法可以使用你的
方法来嘲笑。记住你,我对
模拟测试实践是模糊的,所以我是
可能在这里缺少一个简单的解决方案
。
..在这种情况下,您不需要使用模拟或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);
}
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);
}
In response to the comment...
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.
..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:如何测试事件发送两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!