Skip to main content
Participant
October 29, 2009
Question

Testing Async service that uses callbacks, not Events

  • October 29, 2009
  • 1 reply
  • 595 views

Hello All,

I am curios how others are writing Async tests on a service layer where events are not used?

Ex: calling into a service object - sending a callback method (closure)

####

private function buyItem(id:String, callback:Function

{

DataManager.getInstance().purchaseItem(id, callback);

}

private function callback(success:Boolean, data:Object):void

{

     // Do stuff

}

####

TestBuyItem.as

[Test (order=1, async, timeout="60000")]

public function testBuyItem():void

{

     Async.asyncHandler(this, callback, 20000, null, null);

     DataManager.getInstance().purchaseItem("myitemid", callback);

}

private function callback(success:Boolean, data:Object

{

     // This method gets called and success does equal true - however my test fails

     Assert.assertTrue(success);

}


What am I doing wrong? 

What type of method does the 'callback' need to be?

Is there an Async.waitForCallbackFunctionCall()


I am sure I am doing something wrong, I am just not sure what - thanks for any help you can offer.

--jason

This topic has been closed for replies.

1 reply

Participating Frequently
January 12, 2010

Jason,

Async.asyncHandler(... returns a function. This is your callback. Store a reference to it, and when you receive the result in your callback, execute the function that was returned from Async.asyncHandler.

The asyncHandler signature is "handler(event:Event, passThroughData:Object):void". Just create a dummy event that wraps the data from your callback and invoke the handler.

Hope this helps,

Russ