TestCaseHelper
is a class that helps writing TestCases.
A TestCase
allows proper execution for any kind of test. But if
you want to refactor/break apart your code and simplify your test it disallows
you to handle assertions.
A common example is if you want to test for a interface. Any implementation has to match the rules of the interface. A standardised test for the interface would therefore be a helper. Code could look like this:
Interface
interface MyInterface {
public function addInstance(obj):Void;
public function containsInstance(obj):Void;
public function removeInstance(obj):Void;
}
Implementation #1
import org.as2lib.util.ArrayUtil;
class MyImplementationA implements MyInterface {
private var array:Array;
public fucntion MyImplementationA() {
array = new Array();
}
public function addInstance(obj):Void {
removeInstance(obj);
array.push(obj);
}
public function removeInstance(obj):Void {
ArrayUtil.removeElement(array, obj);
}
public function containsInstance(obj):Boolean {
for (var i:Number = 0; i < array.length; i++) {
if (array[i] === obj) {
return true;
}
}
return false;
}
}
Implementation #2
class MyImplementationB extends MyImplementationA {
public function containsInstance(obj):Boolean {
for (var i:Number = array.length-1; i > 0; i++) {
if (array[i] === obj) {
return true;
}
}
return false;
}
}
Test for the Interface
import org.as2lib.test.unit.TestCaseHelper;
import org.as2lib.test.mock.MockControl;
class TMyInterface extends TestCaseHelper {
public function testAccess(instance:MyInterface) {
var mC:MockControl = new MockControl(Object);
mC.replay();
assertfalse("Instance should not be contained",
instance.containsListener(mC.getMock()));
instance.addInstance(mC.getMock());
assertTrue("Instance should be contained",
instance.containsListener(mC.getMock()));
instance.removeInstance(mC.getMock());
assertfalse("Instance should not be contained",
instance.containsListener(mC.getMock()));
instance.addInstance(mC.getMock());
instance.addInstance(mC.getMock());
instance.removeInstance(mC.getMock());
assertfalse("Instance should not be contained after double adding",
instance.containsListener(mC.getMock()));
mC.verify();
}
}
Test for Implementation A
import org.as2lib.test.unit.TestCase;
class TMyImplementationA extends TestCase {
public function testMyInterface() {
var i:TMyInterface = new TMyInterface(this);
i.testAccess(new MyImplementationA());
}
}
Test for Implementation B
import org.as2lib.test.unit.TestCase;
class TMyImplementationB extends TestCase {
public function testMyInterface() {
var i:TMyInterface = new TMyInterface(this);
i.testAccess(new MyImplementationB());
}
}
new TestCaseHelper()
public function TestCaseHelper(testCase:TestCase)
Constructs a new TestCaseHelper
instance.
testCase | TestCase instance that should be used with all
assertions.
|
static public function blockCollecting(Void):Boolean
Flag to hide from getting collected by org.as2lib.test.unit.TestSuiteFactory.collectAllTestCases
true to block collecting