FlexUnit4 Async Testing Events

When doing Unit testing in FlexUnit 4, you can test Async event firing. This is a tricky test if there are multiple events being fired. The reason it gets tricky is because of the single threaded nature of Flex.

When you simply dispatch one event ofter the other, they are added to the display stack and thus handled in REVERSE order when removed from the stack. However, if you are using remote calls, the stack is given a change to execute, while the remote call is sent off and thus Async events happen in NORMAL order. What makes this really tricky is if you have a combination of local events (reverse order) and remote events (normal order)

Here are some high level examples:

Example 1

dispatchEvent(Event1)

dispatchEvent(Event2)

dispatchEvent(Event3)

Correct Async testing is now done in reverse order, since each event is added to the display stack

Async.proceedOnEvent(this, someEventDispatcher, Event3, checkVars3, 500, null, noEventFired);

Async.proceedOnEvent(this, someEventDispatcher, Event2, checkVars2, 500, null, noEventFired);

Async.proceedOnEvent(this, someEventDispatcher, Event1, checkVars1, 500, null, noEventFired);

Notice the order of the Async.proceedOnEvent is reversed

Example 2

remote call(Event1)

dispatchEvent(Event2)

dispatchEvent(Event3)

Async.proceedOnEvent(this, someEventDispatcher, Event1, checkVars1, 500, null, noEventFired);

Async.proceedOnEvent(this, someEventDispatcher, Event3, checkVars3, 500, null, noEventFired);

Async.proceedOnEvent(this, someEventDispatcher, Event2, checkVars2, 500, null, noEventFired);

Notice the remote call clears properly, but the local calls are reverse order.

So the only way to do this is to understand that if a remote call is executed, you check for it and that if there are any local events, they are placed on a stack until no more events or a remote event takes place and then checked in reverse order.

Tricky….

Follow

Get every new post delivered to your Inbox.