Created
December 15, 2009 16:21
-
-
Save lukesh/257054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package flexUnitTests { | |
import flash.events.Event; | |
import flash.events.EventDispatcher; | |
import flash.events.IEventDispatcher; | |
import flash.utils.setTimeout; | |
import flexunit.framework.Assert; | |
import mx.core.mx_internal; | |
import mx.rpc.AsyncToken; | |
import mx.rpc.IResponder; | |
import mx.rpc.Responder; | |
import mx.rpc.events.ResultEvent; | |
import org.flexunit.assertThat; | |
import org.flexunit.assumeThat; | |
import org.flexunit.async.Async; | |
import org.hamcrest.object.notNullValue; | |
use namespace mx_internal; | |
public class AsyncTest { | |
public static const FIXTURE_RETRIEVE_SUCCESS : String = "fixtureRetrieveSuccess"; | |
public static const FIXTURE_RETRIEVE_FAILURE : String = "fixtureRetrieveFailure"; | |
public static const ASYNC_TIMEOUT : Number = 5000; | |
public static var fixtureObject : Object; | |
public function AsyncTest() { | |
} | |
/** | |
* Simulates an AsyncToken returned from a service call | |
*/ | |
private function mockRetrieveObject(responder : IResponder) : void { | |
var asyncToken : AsyncToken = new AsyncToken(null); | |
var object : Object = {name: "Mock Object", isValid: false}; | |
asyncToken.addResponder(new Responder(function(data : Object) : void { | |
responder.result(data); | |
}, function(info : Object) : void { | |
responder.fault(info); | |
})); | |
setTimeout(function() : void { | |
asyncToken.mx_internal::applyResult(new ResultEvent(ResultEvent.RESULT, false, true, object, asyncToken, null)); | |
}, 200); | |
} | |
/** | |
* Validates object returned from a service call and dispatches | |
* either success or failure. | |
*/ | |
private function getFixtureObject() : IEventDispatcher { | |
var dispatcher : IEventDispatcher = new EventDispatcher(); | |
var responder : Responder = new Responder(function result(data : Object) : void { | |
// | |
// Validate the returned object. | |
// | |
if (data.result.isValid) { | |
fixtureObject = data.result; | |
dispatcher.dispatchEvent(new Event(FIXTURE_RETRIEVE_SUCCESS)); | |
} else { | |
dispatcher.dispatchEvent(new Event(FIXTURE_RETRIEVE_FAILURE)); | |
} | |
}, function fault(info : Object) : void { | |
dispatcher.dispatchEvent(new Event(FIXTURE_RETRIEVE_FAILURE)); | |
}); | |
mockRetrieveObject(Async.asyncResponder(this, responder, ASYNC_TIMEOUT)); | |
return dispatcher; | |
} | |
/** | |
* Each test should have a valid fixture object. If one doesn't | |
* exist, add an async interrupt and get the object. If the object | |
* exists, don't worry about it and let the test continue. | |
*/ | |
[Before(async)] | |
public function setup() : void { | |
if (!fixtureObject) { | |
var dispatcher : IEventDispatcher = getFixtureObject(); | |
Async.proceedOnEvent(this, dispatcher, FIXTURE_RETRIEVE_SUCCESS, ASYNC_TIMEOUT); | |
Async.failOnEvent(this, dispatcher, FIXTURE_RETRIEVE_FAILURE, ASYNC_TIMEOUT); | |
} | |
} | |
/** | |
* Asynchronous test that requires an async setup | |
*/ | |
[Test(async)] | |
public function testSampleMethodAsync() : void { | |
assumeThat(fixtureObject, notNullValue()); | |
var responder : IResponder = new Responder(function(data : Object) : void { | |
assertThat(data.result, notNullValue()); | |
}, function(info : Object) : void { | |
Assert.fail("Could not retrieve object."); | |
}); | |
mockRetrieveObject(Async.asyncResponder(this, responder, ASYNC_TIMEOUT)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment