In order to pass the test you now need to write the actual code. At this point we wrote the test, and once we write the code the test will succeed.
I have implemented the code to call Twitter and retrieve results that includes #FlashAndTheCity hashtagHashTag, see (Listing 7).
Notice that I am using JSON class, which is part of AS3 CoreLib
(http://code.google.com/p/as3corelib/) Figure 13. FlexUnit results view showing green bar
Listing 10.
[Test(async,timeout="5000")]
public function testRetrieveTweetsBasedOnHashTagHashTagFail():void {
classToTestRef = new TwitterHelper();
var EVENT_TYPE:String = "serviceFailure";
classToTestRef.addEventListener( EVENT_TYPE, Async.asyncHandler( this, handleAsyncFaultEvnet, 20000 ), false, 0, true );
classToTestRef.retrieveTweetsBasedOnHashTagHashTag("FlashAndTheCity",
"");
}
private function handleAsyncFaultEvnet(event:Event, passThroughData:Object):
void {
Assert.assertEquals( event.type, "serviceFailure" );
} Listing 11.
public function retrieveTweetsBasedOnHashTagHashTag( hashTagHashTag:String, url:String ):void
{
service = new HTTPService();
service.url = url;
service.resultFormat = "text";
service.addEventListener(ResultEvent.RESULT, onResults);
service.addEventListener(FaultEvent.FAULT, onFault);
var object:Object = new Object();
object.q = hashTagHashTag;
service.senct );
} /**
* Holds the fault method in case the service failed *
* @param event *
*/
private function onFault(event:FaultEvent):void {
service.removeEventListener(ResultEvent.RESULT, onResults);
service.removeEventListener(FaultEvent.FAULT, onFault);
this.dispatchEvent( new TwitterHelperFailureEvent( event.fault.message ) );
}
01/2009 (9) 86
ActionScript Development Flexunit 4 with Test Driven Development
01/2009 (9) 87
Additionally, in order to achieve the test I decided to do a few things such as creating a Value Object (VO) to hold the results and create a custom event that will be dispatched once the results are retrieved. The VO holds most of the properties retrieved from Twitter API, see complete code in Listing 8.
I have created a custom event that will hold the constant event type and allow the passing of the collection of tweets received, see complete code in Listing 9.
Listing 12.
package utils {
import flash.events.Event;
public class TwitterHelperFailureEvent extends Event {
public static const SERVICE_FAILURE:String = "serviceFailure";
public var message:String;
public function TwitterHelperFailureEvent( message:String ) {
this.message = message;
super( SERVICE_FAILURE );
} } }
Listing 13.
[Test(async,timeout="20000")]
public function testRetrieveTweetsBasedOnHashTagHashTag():void {
classToTestRef = new TwitterHelper();
var EVENT_TYPE:String = TwitterHelperSuccessEvent.RETRIEVE_TWEETS;
classToTestRef.addEventListener( EVENT_TYPE, Async.asyncHandler( this, handleAsyncEvnet, 20000 ), false, 0, true );
classToTestRef.retrieveTweetsBasedOnHashTagHashTag("FlashAndTheCity",
"http://search.twitter.com/search.json");
}
[Test(async,timeout="20000")]
public function testRetrieveTweetsBasedOnHashTagHashTagFail():void {
classToTestRef = new TwitterHelper();
var EVENT_TYPE:String = TwitterHelperFailureEvent.SERVICE_FAILURE;
classToTestRef.addEventListener( EVENT_TYPE, Async.asyncHandler( this, handleAsyncFaultEvnet, 20000 ), false, 0, true );
classToTestRef.retrieveTweetsBasedOnHashTagHashTag("FlashAndTheCity", "");
} Listing 14.
[Before]
public function setMeUp():void {
classToTestRef = new TwitterHelper();
} [After]
public function tearMeDown():void {
classToTestRef = null;
}
Figure 14. Result view showing our two tests completed successfully
Figure 15. Retrieve tweets every few seconds User Story
Figure 16. Create TweetListPresenterTester Test Case Class
Figure 17. Run FlexUnit Tests wizard to test testR etriveTweetsEveryFewSeconds method
01/2009 (9) 88
ActionScript Development Flexunit 4 with Test Driven Development
01/2009 (9) 89
Listing 15.
package flexUnitTests {
import flash.events.Event;
import flexunit.framework.Assert;
import org.flexunit.async.Async;
import utils.TwitterHelper;
import utils.TwitterHelperFailureEvent;
import utils.TwitterHelperSuccessEvent;
public class TwitterHelperTester {
// Reference declaration for class to test private var classToTestRef : utils.TwitterHelper;
public function TwitterHelperTester() {
}
[Before]
public function setMeUp():void {
classToTestRef = new TwitterHelper();
} [After]
public function tearMeDown():void {
classToTestRef = null;
}
[Test(async,timeout="20000")]
public function testRetrieveTweetsBasedOnHashTagHashT ag():void
{
var EVENT_TYPE:String = TwitterHelperSuccessEvent.
RETRIEVE_TWEETS;
classToTestRef.addEventListener( EVENT_TYPE, Async.asyncHandler( this,
handleAsyncEvnet, 20000 ), false, 0, true );
classToTestRef.retrieveTweetsBasedOnHashTagHas hTag("FlashAndTheCity", "http://
search.twitter.com/search.json");
}
[Test(async,timeout="20000")]
public function testRetrieveTweetsBasedOnHashTagHashT agFail():void
{
var EVENT_TYPE:String = TwitterHelperFailureEvent.
SERVICE_FAILURE;
classToTestRef.addEventListener( EVENT_TYPE, Async.asyncHandler( this, handleAsyncFaultEvnet, 20000 ),
false, 0, true );
classToTestRef.retrieveTweetsBasedOnHashTagHashTag ("FlashAndTheCity", "");
}
//---
//
// Asynchronous handlers //
//---
private function handleAsyncEvnet(event:Event, passThroughData:Object):void {
Assert.assertEquals( event.type, "retrieveTweets"
);
}
private function handleAsyncFaultEvnet(event:Event, passThroughData:Object):void {
Assert.assertEquals( event.type, "serviceFailure"
);
} } }
Listing 16.
/**
* Success custom event *
* @eventType utils.TwitterHelperSuccessEvent.RETRIEVE _TWEETS
*/
[Event(name="retrieveTweets", type="utils.TwitterHelperS uccessEvent")]
/**
* Failure custome event *
* @eventType utils.TwitterHelperFailureEvent.SERVICE_
FAILURE */
[Event(name="serviceFailure", type="utils.TwitterHelperF ailureEvent")]
01/2009 (9) 88
ActionScript Development Flexunit 4 with Test Driven Development
01/2009 (9) 89
* @eventType utils.TwitterHelperSuccessEvent.RETRIEVE_TWEETS */
* @eventType utils.TwitterHelperFailureEvent.SERVICE_FAILURE */
[Event(name="serviceFailure", type="utils.TwitterHelperFa ilureEvent")]
public class TwitterHelper extends EventDispatcher {
/**
* Holds the service class */
private var service:HTTPService;
* @param hashTagHashTag defined hashtagHashTag to search * @url the tAPI url
* */
public function retrieveTweetsBasedOnHashTagHashTag(
hashTagHashTag:String, url:String ):void {
service = new HTTPService();
service.url = url;
service.resultFormat = "text";
service.addEventListener(ResultEvent.RESULT, onResults);
service.addEventListener(FaultEvent.FAULT, onFault);
private function onResults(event:ResultEvent):void {
service.removeEventListener(ResultEvent.RESULT, onResults);
service.removeEventListener(FaultEvent.FAULT, onFault);
var rawData:String = String( event.result );
var object:Object = JSON.decode( rawData );
var results:Array = object.results as Array;
var collection:Vector.<TweetVO> = new Vector.<TweetVO>;
results.forEach( function callback(item:*, index:
int, array:Array):void { this.dispatchEvent( new TwitterHelperSuccessEvent(
collection ) );
private function onFault(event:FaultEvent):void {
service.removeEventListener(ResultEvent.RESULT, onResults);
service.removeEventListener(FaultEvent.FAULT, onFault);
this.dispatchEvent( new TwitterHelperFailureEvent(
event.fault.message ) );
} } }
01/2009 (9) 90
ActionScript Development Flexunit 4 with Test Driven Development
01/2009 (9) 91
Keep it simple: for the sake of simplicity I am keeping all the classes related to our class under the same package, but feel free to refactor and place the event under an event folder. Also I have not implemented the clone method, which is recommended, but feel free to add these changes.
Run the test again and observe the results, see (Figure 13).
• A test that does not fail succeeds
• Click the Run Completed Button
• Observe the Green Bar that indicates success
One thing to note is that I have set the service to 500 milliseconds; however, the request took longer and still failed. I quickly adjusted the code to wait 5000 milliseconds (5 seconds) to accommodate cases where the network is slow.