Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FlexUnit4 Testing with Parsley: Missing example files
package com.darrenbishop.support.flexunit.async {
import flash.utils.setTimeout;
import mx.core.mx_internal;
import mx.rpc.AsyncResponder;
import mx.rpc.AsyncToken;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
use namespace mx_internal;
public class AsyncUtils {
private static function resultHandler(event:ResultEvent, token:AsyncToken=null):void {
event.token.dispatchEvent(event);
}
private static function applyResult(token:AsyncToken, result:Object):void {
var event:ResultEvent = new ResultEvent(ResultEvent.RESULT, false, true, result, token);
token.mx_internal::applyResult(event);
}
public static function createToken(result:Object, timeout:Number=500):AsyncToken {
var token:AsyncToken = new AsyncToken(null);
setTimeout(applyResult, timeout, token, result);
token.addResponder(new AsyncResponder(resultHandler, null));
return token;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<mx:Object xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:application="com.darrenbishop.crm.directory.application.*">
<!-- Application -->
<application:StubDirectoryService />
<application:GetPersonCommand />
</mx:Object>
package com.darrenbishop.crm.directory.application {
import com.darrenbishop.crm.directory.domain.Person;
import com.darrenbishop.support.flexunit.async.AsyncUtils;
import flash.utils.Dictionary;
import mx.rpc.AsyncToken;
public class StubDirectoryService implements DirectoryService {
private var people:Dictionary = new Dictionary();
public var dodgyPerson:uint;
public var soundPerson:uint;
public function add(...newPeople):void {
for each (var person:Person in newPeople) {
if (people[person.id]) {
throw new Error('A person with id ' + person.id + ' already exists.');
}
people[person.id] = person;
}
}
public function lookupById(id:uint):AsyncToken {
var token:AsyncToken = AsyncUtils.createToken(id == dodgyPerson ? people[soundPerson] : people[id]);
return token;
}
}
}
package com.darrenbishop.crm.directory.application {
import com.darrenbishop.crm.directory.domain.Person;
import flash.utils.Dictionary;
import mx.rpc.AsyncToken;
public class StubLookupTrackingDirectoryService implements DirectoryService {
private var people:Dictionary;
private var lookups:Array;
public function StubLookupTrackingDirectoryService() {
people = new Dictionary();
resetLookups();
}
public function resetLookups():void {
lookups = [];
}
public function add(person:Person):void {
if (people[person.id]) {
throw new Error('A person with id ' + person.id + ' already exists.');
}
people[person.id] = person;
}
public function nextLookup():Person {
return lookups.shift();
}
public function lookupById(id:uint):AsyncToken {
lookups.push(people[id]);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.