Sunday, November 19, 2006

Cairngorm IResponder Async Flexunit testcase

Didn't find any posts addressing FlexUnit async tests using IResponder.
Here is one method of using async flexunit tests with Cairngorm IResponder which involves modifying the flexunit source code.

TestCase.as
------------
public function addResponder(responder : IResponder, timeout : int, passThroughData : Object = null) : IResponder
{
if (asyncTestHelper == null)
{
asyncTestHelper = new AsyncTestHelper(this, testResult);
}
asyncMethods.push({func: responder.result, timeout: timeout, extraData: passThroughData, failFunc: responder.fault, responder: responder});
return asyncTestHelper;
}

AsyncTestHelper.as
---------------------
import mx.rpc.IResponder;
public class AsyncTestHelper implements IResponder
...

public function result(event : Object) : void
{
var wasReallyAsync : Boolean = timer.running;
timer.stop();
//if we already failed don't do anything
if (shouldFail)
return;
objToPass = event;
if (wasReallyAsync)
{
testResult.continueRun(testCase);
}
}

public function fault(event : Object) : void
{
var wasReallyAsync : Boolean = timer.running;
timer.stop();
//if we already failed don't do anything
if (shouldFail)
return;
objToPass = event;
if (wasReallyAsync)
{
shouldFail = true;
testResult.continueRun(testCase);
}
}

Then, in your test case function, use the result of
'this.addResponder(this, 5000)' when you need to pass an IResponder callback

eg.
myDelegate.runAsyncFunction(param, this.addResponder(this, 5000));
instead of
myDelegate.runAsyncFunction(param, this);

2 comments:

The Everynerd said...

This was a huge amount of help to me, thanks for this. One of the few resources on asynchronous testing with Flex.

- the everynerd

Sean Clark Hess said...

A new approach to asynchronous testing in FlexUnit

http://code.seanhess.net/?p=148

[...] FlexUnit has support for async testing built in, but I didn’t particularly like the implementation. I wrote a small class, AsyncTestCase, that extends TestCase and adds support for two functions: start and finish. Basically you call start at the beginning of a series of asynchronous steps, and finish at the end. [...]