Asyncronous test setup with FlexUnit

As part of my evaluation of FlexUnit and ASUnit for continuous integration, one of the rquirements is the ability to have an asynchronous test setup. Lets say you have a class under test that requires some XML in its constructor, and ideally you want to load that XML from an external source. While this doesnt work straight out of the box for flex unit, I found a very simple way to do this. Basically, Flexunit runs its TestCases by executingthe setUp method, then runMiddle() which runs the normal test method or the next asyncronous test. So the trick here is to get runMiddle to execute only when we are ready for it. This question had come up on the Flexcoders mailing list before, and Matt Chotin suggested a possible solution. Here’s mine:


Basically, what you can do is override the runMiddle function to be an empty method, and then when you are ready to actually run your test—in my example here its when an XML file has loaded—simply call super.runMiddle()

override public function setUp():void{
	var loader:URLLoader = new URLLoader();
	loader.addEventListener(Event.COMPLETE, completeHandler);
	var request:URLRequest = new URLRequest("employee.xml");
	loader.load(request);
}
private function completeHandler(event:Event):void {
	var loader:URLLoader = URLLoader(event.target);
	instance = new AsyncSetupTestClass(new XML(loader.data));
	super.runMiddle();
}
override public function runMiddle():void{}

In my simple tests so far this seems to work, but if anyone notices anything out of whack, let me know…

This entry was posted in Flash & Actionscript, Test Driven Development. Bookmark the permalink. Both comments and trackbacks are currently closed.