For some reason, addAsync chaining in a flexunit test as described in this article utterly fails to work when I try to do it.
public function testWhatever():void { var cont:EventDispatcher = new EventDispatcher(); cont.addEventListener('continue', addAsync(verifyFirst, 1000)); cont.dispatchEvent(new Event('continue')); } private function verifyFirst(e:Event):void { var cont:EventDispatcher = new EventDispatcher(); cont.addEventListener('continue', addAsync(verifySecond, 1000)); cont.dispatchEvent(new Event('continue')); } private function verifySecond(e:Event):void { assertTrue(true); }
If I run this test, verifyFirst gets called but verifySecond does not. I’m assuming this is a bug in flexunit … is there a workaround?
I did some more research and found that this is indeed a bug in flexunit, which looks to be fixed in the next release. The workaround I found was to instead use
Application.application.callLaterto dispatch the second event.This question was inspired by an attempt to inspect the state of an object after everything in Flash’s event queue had been processed. I discovered a simpler way to accomplish this without messing with
EventDispatchers.Make the following call the end of the first part of the test when you want the event queue to be processed.
Application.application.callLater(addAsync(phaseTwo, 1000, [args…]), [null]);With the
phaseTwofunction having the following signature.private function phaseTwo(e:Event, args:Array):voidewill be passed anullobject. This is necessary to be compatible withaddAsync.