Skip to content

Instantly share code, notes, and snippets.

@Stray
Created January 12, 2011 20:41
Show Gist options
  • Save Stray/776837 to your computer and use it in GitHub Desktop.
Save Stray/776837 to your computer and use it in GitHub Desktop.
A typical service test
package com.client.app.feature.restricted.model {
public class AdminDataLoadingURLVarsFactorySupport extends AdminDataLoadingURLVarsFactory {
public function AdminDataLoadingURLVarsFactorySupport(keyIsGood:Boolean = true) {
if(keyIsGood){
fingerprintKey = new AdminKeyGoodSupport();
}
else
{
fingerprintKey = new AdminKeyBadSupport();
}
queryLookup = new AdminDataQueryKeyLookup();
}
}
}
package com.client.app.feature.restricted.services.accounts {
import asunit.framework.TestCase;
import org.robotlegs.mvcs.Actor;
import com.client.app.feature.restricted.services.AdminDataLoadingService;
import flash.events.EventDispatcher;
import com.client.app.feature.api.events.AdminDataServiceUpdateEvent;
import com.client.app.feature.restricted.model.AdminDataLoadingURLVarsFactorySupport;
import com.client.app.feature.restricted.model.IAdminXMLToModelProcessor;
import mockolate.prepare;
import mockolate.nice;
import mockolate.stub;
import mockolate.verify;
import org.hamcrest.core.anything;
import org.hamcrest.core.not;
import org.hamcrest.object.equalTo;
import org.hamcrest.object.nullValue;
import org.hamcrest.object.strictlyEqualTo;
import org.hamcrest.object.instanceOf;
import flash.events.Event;
import flash.events.IEventDispatcher;
public class AccountDataLoadingServiceTest extends TestCase {
private var instance:AccountDataLoadingService;
private const BAD_URL:String ="http://www.fhdfdshfjkdshfka.fds.cdsc";
private const GOOD_URL:String = "http://master.client.co.uk/adminScripts/getData.php5";
public function AccountDataLoadingServiceTest(methodName:String=null) {
super(methodName)
}
override public function run():void{
var mockolateMaker:IEventDispatcher = prepare(IAdminXMLToModelProcessor);
mockolateMaker.addEventListener(Event.COMPLETE, prepareCompleteHandler);
}
private function prepareCompleteHandler(e:Event):void{
IEventDispatcher(e.target).removeEventListener(Event.COMPLETE, prepareCompleteHandler);
super.run();
}
override protected function setUp():void {
super.setUp();
instance = new AccountDataLoadingService();
instance.eventDispatcher = new EventDispatcher();
instance.loadingURLVarsFactory = new AdminDataLoadingURLVarsFactorySupport();
instance.adminXMLToModelProcessor = nice(IAdminXMLToModelProcessor)
}
override protected function tearDown():void {
super.tearDown();
instance = null;
}
public function testInstantiated():void {
assertTrue("instance is AccountDataLoadingService", instance is AccountDataLoadingService);
}
public function testIsService():void{
assertTrue("instance is robotlegs Actor", instance is Actor);
}
public function testIsAdminDataLoadingService():void{
assertTrue("instance is AdminDataLoadingService", instance is AdminDataLoadingService);
}
public function testFailure():void {
assertTrue("Failing test", true);
}
public function test_fires_adminDataLoadingErrorEvent_with_bad_url():void {
var handler:Function = addAsync(check_fires_adminDataLoadingErrorEvent_with_bad_url, 2000);
instance.eventDispatcher.addEventListener(AdminDataServiceUpdateEvent.DATA_LOADING_ERROR, handler);
instance.loadingScriptPath = BAD_URL;
instance.loadData();
}
private function check_fires_adminDataLoadingErrorEvent_with_bad_url(e:AdminDataServiceUpdateEvent):void {
assertEquals('event is correct type', AdminDataServiceUpdateEvent.DATA_LOADING_ERROR, e.type);
}
public function test_fires_complete_event_with_good_url():void {
var handler:Function = addAsync(check_fires_complete_event_with_good_url, 10000);
instance.eventDispatcher.addEventListener(AdminDataServiceUpdateEvent.DATA_LOADING_COMPLETED, handler);
instance.loadingScriptPath = GOOD_URL;
instance.loadData();
}
private function check_fires_complete_event_with_good_url(e:AdminDataServiceUpdateEvent):void {
assertEquals('event is correct type', AdminDataServiceUpdateEvent.DATA_LOADING_COMPLETED, e.type);
check_has_run_process_data_on_processing_service();
}
public function check_has_run_process_data_on_processing_service():void {
verify(instance.adminXMLToModelProcessor).method('processAccountData').args(instanceOf(XML));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment