Remote Services testing using flexunit4

This post was moved to http://srinichekuri.com/2011/01/14/remote-service-testing-flexunit4/

**********************************************************************

I have been working to test the service layer in Flex. I tried using flexUnit to do this and after some initial hickups was able to figure out a way to test the remoteservices using Asynchronous functionality provided by the new FlexUnit4 framework.

This is a simple logic I have followed to get this working:
Step1: addEventListener to call resultHandler method when the result is triggered by service. ResultHandler method dispatchs an “ServiceCallComplete” event.
Step2: Async.proceedOnEvent will be looking for “ServiceCallComplete” event.

Here is the sample code I have created:

package testsuites.tests
{
import flash.events.Event;
import flash.events.EventDispatcher;

import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;

public class TestServiceCall
{
private static var dispatcher:EventDispatcher;
private static var service:RemoteObject;

[Before]
public function setUp():void
{
}

[After]
public function tearDown():void
{
}

[BeforeClass]
public static function setUpBeforeClass():void
{
//Initializing RemoteService
service = new TestRemoteService();
dispatcher = new EventDispatcher();

}

[AfterClass]
public static function tearDownAfterClass():void
{
service = null
}

[Test(async, description="Call Service", order=1)]
public function testGetRequest():void{
service.addEventListener(ResultEvent.RESULT,resultHandler);
service.addEventListener(FaultEvent.FAULT,faultHandler);
//Calling testCall method on TestRemoteService
service.testCall("123");
}

[Test(async, description="Async Call to look for completion of call",order=2)]
public function testResponse():void{
Async.proceedOnEvent(this,dispatcher,"ServiceCallComplete",10000);
}

[Test(description="Test response",order=3)]
public function testResponse():void{
//Start testing the result from here
}

public function resultHandler(event:ResultEvent):void{
actualOutputObject = event.result;
dispatcher.dispatchEvent(new Event("ServiceCallComplete"));
}

public function faultHandler(event:FaultEvent):void{

}
}
}