Using Signals in FlexUnit 4 Asynchronous tests

Posted by Kristofer Joseph Thu, 01 Apr 2010 20:32:00 GMT

Recently I switched over to using Robbert Penners great as3 Signals project when sending application level events for a number of reasons. You should really read about the benefits here. The rub came when I wanted to write my tests ( first ) and realized that FlexUnit 4 was tied to flash events. Read on to see how we can get around this limitation.

Example Signal

	package com.developsigner.signals
	{
		import org.osflash.signals.Signal;
		
		/**
		 * Class.
		 * 
		 * @langversion ActionScript 3.0
		 * @playerversion Flash 10.0
		 * 
		 * @author Kristofer Joseph
		 * @since  01.04.2010
		 */
		public class SuccessSignal extends Signal
		{
			//--------------------------------------
			//  CONSTRUCTOR
			//--------------------------------------

			/**
			 * @constructor
			 */
			public function SuccessSignal()
			{
				super(Boolean);
			}	
		}
	}

Example Async test

	package com.developsigner.service
	{
		import org.flexunit.Assert;
		import org.flexunit.async.Async;
		import flash.events.Event;
		import com.developsigner.signals.successSignal;
		/**
		 * Class.
		 * 
		 * @langversion ActionScript 3.0
		 * @playerversion Flash 10.0
		 * 
		 * @author Kristofer Joseph
		 * @since  01.04.2010
		 */
		public class ExampleSignalAsyncTest
		{
			/**
			 * @private
			 */
			protected var successSignal:SuccessSignal;

			[Before]
			public function setUp():void
			{
				successSignal = new SuccessSignal();
			}

			[After]
			public function tearDown():void
			{
				successSignal = null;
			}

			[Test(async,"Async test using signals")]
			public function asyncSignalsTest():void
			{
				var passThrough:Object = new Object();
				var asyncHandler:Function = Async.asyncHandler(this,successHandler,500,passThrough,timeoutHandler);
				
				successSignal.add
				( 
					//CHANGED: Anonymous functions are automatically passed the signal payload 
					//therefore can be used in the pass through object
					function(success:Boolean):void
					{ 
						passThrough.success = success; 
						//CHANGED: Only lame thing here is the asyncHandler MUST have an event passed to it.
						// It passes on this event with the pass through object to the success handler. 
						// We make a dummy event for this. 
						// We then make our assertions on the pass through object.
						asyncHandler( new Event("Success") ) 
					} 
				);
				
				//Agreed this is completely contrived, but it is just meant to illustrate the point
				successSignal.dispatch(true);
			}

			public function successHandler(event:Event,passThrough:Object):void
			{
				trace("SUCCES:" + passThrough.success);
				Assert.assertTrue(passThrough.success);
			}

			public function timeoutHandler(passThrough:Object):void
			{
				Assert.fail("Time out occurred");
			}

		}

	}

I intend to add a new method to use in the future that takes a signal instead of an event, but this will be part of ASUnit not FlexUnit.

7 comments | Tags , , , , , , | atom

Comments

Leave a response

  1. Robert Penner
    about 1 hour later:

    Thanks for the example. Quick thing: three of the URLs in the article point to this page; looks like typos.

  2. Robert Penner
    about 1 hour later:

    I suggest mentioning the async handler limitation to the FlexUnit team so they can provide an API and remove the need for this workaround.

  3. Kristofer Joseph
    about 1 hour later:

    @robertpenner Good catch. Fixed the links. Damn Javascript rich text editors.

  4. Michael Labriola
    about 1 hour later:

    Nice article. As Robert suggested, I think we can absolutely provide you a better way to do this in new versions. We are working on making Async better for 4.1, which will be released next month. I promise this feedback will help us shape it.

    Thanks again, Mike

  5. Kristofer Joseph
    about 3 hours later:

    @Michael Labriola Good to hear. I would be happy with just not making the event required.

  6. Mr Han
    3 months later:

    is this event over or still does exist ?

  7. Mr Han
    3 months later:

    asd

Leave a comment