Creating a Mock delegate from an InterfaceIn this example we will use this simple interface. package mockas3.examples {
import flash.events.IEventDispatcher;
public interface Example extend IEventDispatcher {
function acceptNumber( value:Number ):void;
function giveString():String;
function justCall():void;
function dispatchMyEvent():void;
function callWithRest(...rest):void;
}
}For each method we now need to delegate the calls to an instance of Mock. package com.anywebcam.mock.examples {
import com.anywebcam.mock.Mock;
import flash.events.Event;
import flash.events.EventDispatcher;
public class MockExample implements Example {
public var mock:Mock;
public function MockExample( ignoreMissing:Boolean = false ) {
mock = new Mock( this, ignoreMissing );
}
public function acceptNumber( value:Number ):void {
mock.acceptNumber( value );
}
public function giveString():String {
return mock.giveString();
}
public function optional( ...rest ):void {
mock.invokeMethod('optional', rest);
}
public function justCall():void {
mock.justCall();
}
public function callWithRest(...rest):void {
mock.invokeMethod('callWithRest', rest);
}
public function dispatchMyEvent():void {
mock.dispatchMyEvent();
}
public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void {
mock.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void {
mock.removeEventListener(type, listener, useCapture);
}
public function dispatchEvent(event:Event):Boolean {
return mock.dispatchEvent(event);
}
public function hasEventListener(type:String):Boolean {
return mock.hasEventListener(type);
}
public function willTrigger(type:String):Boolean {
return mock.willTrigger(type);
}
}
}Every call is passed on the mock as exactly the same method name, and the same arguments. The only exception is for methods that accept ...rest parameters. The two examples below show how to use Mock#invokeMethod with rest parameters and in combination with normal parameters. public function callWithRest(...rest):void {
mock.invokeMethod('callWithRest', rest);
}
public function callWithParametersAndRest(a:int, b:String, ...rest):void {
mock.invokeMethod('callWithRest', [a, b].concat(rest));
}Creating a Mock delegate from a ClassIs the same as for an interface, although care must be taken to override every method so as not to use the default behaviour as provided by the Class. In this example we create a Mock for the flash.media.Sound class. package mockas3.examples {
import com.anywebcam.mock.Mock;
import flash.media.Sound;
public class SoundMock extends Sound {
public var mock:Mock;
public function SoundMock(stream:URLRequest = null, context:SoundLoaderContext = null) {
mock = new Mock(this);
}
override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
mock.addEventListener(type, listener, useCature, priority, useWeakReference);
}
override public function close():void {
mock.close();
}
override public function dispatchEvent(event:Event):Boolean {
return mock.dispatchEvent(event);
}
override public function extract(target:ByteArray, length:Number, startPosition:Number = -1):Number {
return mock.extract(target, length, startPosition);
}
override public function hasEventListener(type:String):Boolean {
return mock.hasEventListener(type);
}
override public function load(stream:URLRequest, context:SoundLoaderContext = null):void {
mock.load(stream, context);
}
override public function play(startTime:Number = 0, loops:int = 0, sndTransform:SoundTransform = null):SoundChannel {
return mock.play(startTime, loops, sndTransform);
}
override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
return mock.removeEventListener(type, listener, useCapture);
}
override public function toString():String {
return mock.toString();
}
override public function valueOf():Object {
return mock.valueOf();
}
override public function willTrigger(type:String):Boolean {
return mock.willTrigger(type);
}
}
}
|
You may want to try mockito for flex, it's expectations based and supports dynamic classes http://bitbucket.org/loomis/mockito-flex
Is it possible with the update made on Oct 13th to create the mocks automatically? With the proxy generation? If so, can you provide some documentation?