How to Use Asynchronous Test Methods with FlexUnit 4

July 15th, 2009 by Ben

An essential part of Agile Development is to embrace the use of unit testing throughout the development of your project. Unit tests give the programmer to ability to run automated tests on their code at anytime to ensure that any refactoring or added code has not broken what was previously written. This luxury is a welcome proposition for many programmers.

However, this has proven to be problematic for Flex developers for a variety of reasons. The first reason is that because Flex/Flash is a presentation tier technology a great deal of testing requires user interaction. Another problem is that Flex/Flash is a client-side technology and therefore data saving and retrieval is an asynchronous process. In this post, I'm going to cover on how to handle the latter with FlexUnit 4.

With the first implementation of FlexUnit, a programmer could handle an asynchronous test by using a combination of timers and the built in addSync method of FlexUnit. Here's an example pulled from this blog post:

private var _timer:Timer;
 
override public function tearDown():void {
    _timer.stop();
    _timer = null;
}
 
public function testTimer():void {
    _timer = new Timer(3000, 1);
    _timer.addEventListener(TimerEvent.TIMER, incrementCount);
    _timer.addEventListener(TimerEvent.TIMER_COMPLETE, addAsync(verifyCount, 3500));
    _timer.start();
}

As you may have noticed, this code is a bit confusing. Basically what is happening is that FlexUnit is being told to wait 3.5 seconds before checking to see if "verifyCount" has been invoked. While this code works, it's a bit clunky and not very intuitive. You have to create a timer, add event listeners for the timer, and then actually tell the timer to run. This may seem to be a trivial complaint, but when you have to write a series of tests with dependencies on previously written async tests this can get pretty tedious pretty quickly.

With Flex Unit 4 asynch test functions have been streamlined to look something like this:

 
[Test(async,timeout="3000")]
public function UserRegistration():void{
 
	Async.handleEvent(this, _service, ServiceEvent.USER_REGISTERED, onUserRegistered);
	_service.registerUser(_testEmailStr);
 
}
 
private function onUserRegistered(event:ServiceEvent, param2:*):void{
	_testUser = UserVO.fromObject(event.data);
	Assert.assertNotNull(_testUser);
	Assert.assertTrue(_testUser.email == _testEmailStr);
 
}

In this example, we have a test function called "UserRegistration." This function will test if my async call to the server to register a user has worked. Notice the the "Async.handleEvent" line of code. In this line we are telling Flex Unit 4 to listen for an event that "_service" will be broadcasting. This line of code also tells FlexUnit to invoke the handler function "onUserRegistered" if "_service" does broadcast this event. The meta tag line above the declaration for UserRegistration also specifies that this test function should issue a failure notification if the function takes longer than 3 seconds to complete.

As for the actual "onUserRegistered" handler, we run an assertion to make sure the new VO got parsed correctly. Once that is done, we also make sure the properties of the new VO were set correctly.

For more information about FlexUnit 4 be sure to checkout Digital Primate's FlexUnit in 360 seconds post as well as O'Reilly's post. If you would like FlexUnit 4 to be directly integrated with your copy of Flash Builder today check this link out as well.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • DZone
  • Fark
  • LinkedIn
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • RSS
  • email

8 Responses to “How to Use Asynchronous Test Methods with FlexUnit 4”

  1. [...] An essential part of Agile Development is to embrace the use of unit testing throughout the development of your project. Unit tests give the programmer to ability to run automated tests on their code at anytime to ensure that any refactoring or added code has not broken what was previously written. Here is the original post: How to Use Asynchronous Test Methods with FlexUnit 4 [...]

  2. [...] is the first actual AIR application with the Creational Patterns portion of the catalog [...] How to Use Asynchronous Test Methods with FlexUnit 4 – themorphicgroup.com 07/15/2009 An essential part of Agile Development is to embrace the use of [...]

  3. Mark says:

    Thanks for posting. This was very helpful.

  4. Nek says:

    Thanks for the great post! Just used Async.handleEvent(..). Works great.
    The only strange thing is param2:*. Where does it come from?

  5. Tim Oxley says:

    Agreed. Why have you got param2:* in there?

  6. Tim Oxley says:

    Turns out it will give an “ArgumentError: Error#1063: Argument count mismatch on … ” if you don’t allow for that parameter

    note you CAN do this in an async test by simply adding listeners, and Asserting in an anonymous function:

    object.addEventListener(“eventName”,
    function(event:Event):void {
    Assert.assertTrue();
    });

  7. dharshini says:

    could any one tell me that how to write a test case for unit testing in flex unit using flex builder 3.0

  8. dharshini says:

    explain abt unit testing in flex unit

Leave a Reply