Skip to content

Instantly share code, notes, and snippets.

@newtriks
Created September 2, 2011 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newtriks/1188285 to your computer and use it in GitHub Desktop.
Save newtriks/1188285 to your computer and use it in GitHub Desktop.
NetConnection service Class UnitTest example
package
{
import asunit.framework.TestCase;
import com.newtriks.restricted.modules.streamingservicemodule.api.signals.NetConnectionSuccessful;
import com.newtriks.restricted.shell.api.signals.RecorderError;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import mockolate.nice;
import mockolate.prepare;
import mockolate.verify;
import org.hamcrest.object.equalTo;
import org.robotlegs.mvcs.Actor;
public class NetConnectionServiceTest extends TestCase
{
private var instance:NetConnectionService;
private var netConnectionWasSuccessReceived:Boolean;
private var netConnectionErrorCode:uint;
public function NetConnectionServiceTest(methodName:String=null)
{
super(methodName)
}
override public function run():void
{
var mockolateMaker:IEventDispatcher=prepare(NetConnection);
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 NetConnectionService();
instance.eventDispatcher=new EventDispatcher();
instance.netConnectionSuccessful=new NetConnectionSuccessful();
instance.errorSignal=new RecorderError();
var connection:NetConnection=nice(NetConnection);
instance.connection=connection;
}
override protected function tearDown():void
{
super.tearDown();
instance=null;
}
public function testInstantiated():void
{
assertTrue("instance is NetConnectionService", instance is NetConnectionService);
}
public function testIsService():void
{
assertTrue("instance is robotlegs Actor", instance is Actor);
}
public function testFailure():void
{
assertTrue("Failing test", true);
}
public function testConnectCallsOnNetConnection():void
{
instance.connect("");
verify(connection).method("connect").anyArgs();
}
public function testFiresNetConnectionServiceEventCONNECTEDWhenConnected():void
{
stub(connection).method("connect").anyArgs().dispatches(new NetStatusEvent(NetStatusEvent.NET_STATUS, true, false, {code:"NetConnection.Connect.Success"}));
instance.netConnectionSuccessful.add(netConnected);
netConnectionWasSuccessReceived=false;
addAsync(null, 1000, verifyNetConnectionConnected);
instance.connect("");
}
public function verifyNetConnectionConnected(event:Event):void
{
assertTrue("NetConnectionService status is NetConnectionService.CONNECTED", instance.status==NetConnectionService.CONNECTED);
}
public function testFiresNetConnectionServiceEventFAILEDWhenFailsToConnect():void
{
stub(connection).method("connect").anyArgs().dispatches(new NetStatusEvent(NetStatusEvent.NET_STATUS, true, false, {code:"NetConnection.Connect.Failed"}));
instance.errorSignal.add(netFailed);
netConnectionErrorCode=0;
addAsync(null, 1000, verifyNetConnectionFailed);
instance.connect(DUDD_CONNECTION_URL);
}
public function verifyNetConnectionFailed(event:Event):void
{
assertTrue("NetConnectionService status is NetConnectionService.FAILED", instance.status==NetConnectionService.FAILED);
assertTrue("NetConnectionService failure code reported is 2", netConnectionErrorCode==2);
}
private function netConnected(connection:NetConnection):void
{
netConnectionWasSuccessReceived=true;
}
private function netFailed(code:uint, message:String):void
{
netConnectionErrorCode=code;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment