views:

578

answers:

1

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?

+1  A: 

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.callLater to dispatch the second event.

private function verifyFirst(e:Event):void {
    var cont:EventDispatcher = new EventDispatcher();
    cont.addEventListener("continue", addAsync(verifySecond, 1000));
    Application.application.callLater(cont.dispatchEvent,
       [new Event("continue")]);
}

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 phaseTwo function having the following signature.

private function phaseTwo(e:Event, args:Array):void

e will be passed a null object. This is necessary to be compatible with addAsync.

Tmdean