📜 ⬆️ ⬇️

Asynchronous callback functions in FlexUnit

Free translation of a post - Asynchronous Callback Functions in FlexUnit

This post is about how to test methods using FlexUnit tools that use callback functions, as a solution it is proposed to use a class that allows you to add callback functions to the test.

FlexUnit allows asynchronous testing using the Async class. This class works great for testing events, but does not support callback functions. A typical asynchronous event test looks like this:

[Test(async, description="Async Example")] public function testFancyInstanceFiresComplete():void { var asyncHandler:Function = Async.asyncHandler(this, doStuffComplete, 500); fancyInstance.addEventListener(FancyEvent.COMPLETE, asyncHandler, false, 0, true); fancyInstance.doStuff(); } 

')
If you need a test method using a callback function, you can write this:

 [Test(async, description="Async Callback Example")] public function testFancyInstanceFiresComplete():void { var asyncHandler:Function = Async.asyncHandler(this, doStuffComplete, 500); fancyInstance.doStuff(asyncHandler); } 


However, this can not be the case for the event event object. One possible solution is to use the callbacks:

However, this can cause runtime errors when the parameter of the callback function cannot be converted to an expected object of type Event. One possible solution is to use a special class to create callbacks:

 package de.betriebsraum.utils.tests { import flash.events.Event; import flash.events.EventDispatcher; import org.flexunit.async.Async; public class AsyncUtil extends EventDispatcher { public static const ASYNC_EVENT:String = "asyncEvent"; private var _testCase:Object; private var _callback:Function; private var _passThroughArgs:Array; private var _callbackArgs:Array; public function AsyncUtil(testCase:Object, callback:Function, passThroughArgs:Array = null) { _testCase = testCase; _callback = callback; _passThroughArgs = passThroughArgs; } public static function asyncHandler(testCase:Object, callback:Function, passThroughArgs:Array = null, timeout:Number = 1500):Function { var asyncUtil:AsyncUtil = new AsyncUtil(testCase, callback, passThroughArgs); asyncUtil.addEventListener(ASYNC_EVENT, Async.asyncHandler(testCase, asyncUtil.asyncEventHandler, timeout)); return asyncUtil.asyncCallbackHandler; } public function asyncEventHandler(ev:Event, flexUnitPassThroughArgs:Object = null):void { if (_passThroughArgs) { _callbackArgs = _callbackArgs.concat(_passThroughArgs); } _callback.apply(null, _callbackArgs); } public function asyncCallbackHandler(...args:Array):void { _callbackArgs = args; dispatchEvent(new Event(ASYNC_EVENT)); } } } 


With the help of this class, you can safely add callback functions without receiving an error.

 [Test(async, description="Async Callback Example")] public function testFancyInstanceFiresComplete():void { var asyncHandler:Function = AsyncUtil.asyncHandler(this, doStuffComplete); fancyInstance.doStuff(asyncHandler); } 

Source: https://habr.com/ru/post/52529/


All Articles