Unit test to test live data coming out of a Flex Service call – FlexUnit re-visited :)
Here is an attempt to explain the way(one of the ways) to write unit tests for a service call(via HTTPService,WebService or RemoteObject) which returns live data.
1) Adobe Flash Builder4 beta.
2) Please import the project attached here
Note: Refresh the service EmployeeService in the Data/Services panel for the project to compile succesfully
I have an xml file called employees.xml, i would like to display the details of all the employees available in my application.I use HTTPService to get the data to the flex application. Using the Data centric application dvevelopment(DCRAD) wizards i generate a new HTT P Service with an operation called getEmployees( refer to EmployeeService.as and _Super_EmployeeService.as in services folder in the attached project).
My job is to find out if the function getEmployees is returning the right data. So i perform an unit test to check if the number of employees is correct. FYI the getEmployees function returns me an AsyncToken whose result property will be populated with the result of the operation when the server receives the response.
Please find the code for the getEmployees function below:
public function getEmployees() : AsyncToken
var _internal_operation:AbstractOperation = _serviceControl.getOperation(“getEmployees”);
var _internal_token:AsyncToken = _internal_operation.send() ;
Using the FlexUnit integration in Flash Builder i create a test case to test the above function.
Please refer to creation of test case class from https://balajisridhar.wordpress.com/2009/06/06/a-jolly-ride-with-flexunit-in-flash-builder4/
In the Test Case wizard, name the class as ‘EmpServiceTest’ and select ‘Class to test’ as EmployeeService.as and select the ‘Method to Test’ as getEmployees.
In the EmpServiceTest class generated by Flash Builder add the following code in the method ‘testGetEmployees’
asynToken = classToTestRef.getEmployees();
myTimer = new Timer(3000,1);
Please find this code in the EmpServiceTest.as in the flexUnitTests folder in the attached project
There are 2 things which i am performing here:
a) Calling the getEmployees function from the reference(classToTestRef) of EmployeeService.as class.
b) Trying to hold the application untill my tests run by setting the timer to a particular time(3500 s) and calling a function resultHandler1.
Now create a new function resultHandler1 by pasting the following code in ‘EmpServiceTest.as’:
public function resultHandler1(event:TimerEvent):void
var arrColl:ArrayCollection =asynToken.result as ArrayCollection;
I am trying to process the result from the AsyncToken and checking if the number of employees in the result is 26.To do this i assign the asyncToken’s result to an ArrayCollection and i am checking for the length property of the ArrayCollection.
Run the test by context clicking on ‘EmpServiceTest’ and select ‘Execute FlexUnit Tests’. The test passes as the number of employees are 26 and the result is displayed in the Flash Builder’s ‘FlexUnit Results’ panel.
“HOPE THIS HELPS “ 🙂