| Package | asunit.framework |
| Class | public class TestCase |
| Inheritance | TestCase Assert flash.events.EventDispatcher |
| Implements | Test |
| Subclasses | AsynchronousTestCase, RemotingTestCase, TestCaseExample, TestSuite |
setUptearDown.
public class MathTest extends TestCase {
private var value1:Number;
private var value2:Number;
public function MathTest(methodName:String=null) {
super(methodName);
}
override protected function setUp():void {
super.setUp();
value1 = 2;
value2 = 3;
}
}
assertTrue with a boolean, or assertEquals
with two primitive values that should match.
public function testAdd():void {
var result:Number = value1 + value2;
assertEquals(5, result);
}
| Property | Defined by | ||
|---|---|---|---|
| context : DisplayObjectContainer | TestCase | ||
| fName : String | TestCase | ||
| isComplete : Boolean | TestCase | ||
| result : TestListener | TestCase | ||
| testMethods : Array | TestCase | ||
| Method | Defined by | ||
|---|---|---|---|
|
TestCase(testMethod:String = null)
Constructs a test case with the given name.
| TestCase | ||
![]() |
assertEquals(... args):void
[static]
Asserts that two objects are equal.
| Assert | |
![]() |
assertEqualsArrays(... args):void
[static]
Asserts that two arrays have the same length and contain the same
objects in the same order.
| Assert | |
![]() |
assertEqualsArraysIgnoringOrder(... args):void
[static]
Asserts that two arrays have the same length and contain the same
objects.
| Assert | |
![]() |
assertEqualsFloat(... args):void
[static]
Asserts that two numerical values are equal within a tolerance range.
| Assert | |
![]() |
assertFalse(... args):void
[static]
Asserts that a condition is false.
| Assert | |
![]() |
assertNotNull(... args):void
[static]
Asserts that an object isn't null.
| Assert | |
![]() |
assertNotSame(... args):void
[static]
Asserts that two objects do not refer to the same object.
| Assert | |
![]() |
assertNull(... args):void
[static]
Asserts that an object is null.
| Assert | |
![]() |
assertSame(... args):void
[static]
Asserts that two objects refer to the same object.
| Assert | |
![]() |
assertThrows(errorType:Class, block:Function):void
[static]
Asserts that the provided block throws an exception that matches
the type provided.
| Assert | |
![]() |
assertTrue(... args):void
[static]
Asserts that a condition is true.
| Assert | |
|
asyncOperationComplete(async:AsyncOperation):void
| TestCase | ||
|
asyncOperationTimeout(async:AsyncOperation, duration:Number):void
| TestCase | ||
|
countTestCases():int
Counts the number of test cases executed by run(TestResult result).
| TestCase | ||
![]() |
fail(message:String):void
[static]
Fails a test with the given message.
| Assert | |
|
getContext():DisplayObjectContainer
Returns the visual
DisplayObjectContainer that will be used by
addChild and removeChild helper methods. | TestCase | ||
|
getCurrentMethod():String
| TestCase | ||
|
getIsComplete():Boolean
| TestCase | ||
|
getName():String
Gets the name of a TestCase
| TestCase | ||
| TestCase | |||
|
getTestMethods():Array
| TestCase | ||
|
run():void
A convenience method to run this test, collecting the results with
either the TestResult provided or a default, new TestResult object.
| TestCase | ||
|
runBare():void
Runs the bare test sequence.
| TestCase | ||
|
setContext(context:DisplayObjectContainer):void
| TestCase | ||
|
setName(name:String):void
Sets the name of a TestCase
| TestCase | ||
|
setResult(result:TestListener):void
| TestCase | ||
|
toString():String
Returns a string representation of the test case
| TestCase | ||
| Method | Defined by | ||
|---|---|---|---|
|
addAsync(handler:Function = null, duration:Number):Function
Called from within
setUp or the body of any test method. | TestCase | ||
|
addChild(child:DisplayObject):DisplayObject
Helper method for testing
DisplayObjects. | TestCase | ||
|
cleanUp():void
Override this method in Asynchronous test cases
or any other time you want to perform additional
member cleanup after all test methods have run
| TestCase | ||
|
Creates a default TestResult object
| TestCase | ||
|
removeChild(child:DisplayObject):DisplayObject
Helper method for removing added
DisplayObjects. | TestCase | ||
|
runTearDown():void
| TestCase | ||
|
setTestMethods(methodNodes:XMLList):void
| TestCase | ||
|
setUp():void
Sets up the fixture, for example, instantiate a mock object.
| TestCase | ||
|
tearDown():void
Tears down the fixture, for example, delete mock object.
| TestCase | ||
| Constant | Defined by | ||
|---|---|---|---|
| DEFAULT_TIMEOUT : int = 1000 [static]
| TestCase | ||
| PRE_SET_UP : int = 0 [static]
| TestCase | ||
| RUN_METHOD : int = 2 [static]
| TestCase | ||
| SET_UP : int = 1 [static]
| TestCase | ||
| TEAR_DOWN : int = 3 [static]
| TestCase | ||
| context | property |
protected var context:DisplayObjectContainer
| fName | property |
protected var fName:String
| isComplete | property |
protected var isComplete:Boolean
| result | property |
protected var result:TestListener
| testMethods | property |
protected var testMethods:Array
| TestCase | () | constructor |
public function TestCase(testMethod:String = null)
Constructs a test case with the given name.
Be sure to implement the constructor in your own TestCase base classes.
Using the optional testMethod constructor parameter is how we
create and run a single test case and test method.
testMethod:String (default = null) |
| addAsync | () | method |
protected function addAsync(handler:Function = null, duration:Number):Function
Called from within setUp or the body of any test method.
Any call to addAsync, will prevent test execution from continuing
until the duration (in milliseconds) is exceeded, or the function returned by addAsync
is called. addAsync can be called any number of times within a particular
test method, and will block execution until each handler has returned.
Following is an example of how to use the addAsync feature:
public function testDispatcher():void {
var dispatcher:IEventDispatcher = new EventDispatcher();
// Subscribe to an event by sending the return value of addAsync:
dispatcher.addEventListener(Event.COMPLETE, addAsync(function(event:Event):void {
// Make assertions nsideyour async handler:
assertEquals(34, dispatcher.value);
}));
}
public function testDispatcher():void {
var dispatcher:IEventDispatcher = new EventDispatcher();
dispatcher.addEventListener(Event.COMPLETE, addAsync());
}
addAsync block.
Parameters
handler:Function (default = null) |
|
duration:Number |
Function |
| addChild | () | method |
protected function addChild(child:DisplayObject):DisplayObject
Helper method for testing DisplayObjects.
This method allows you to more easily add and manage DisplayObject
instances in your TestCase.
If you are using the regular TestRunner, you cannot add Flex classes.
If you are using a FlexRunner base class, you can add either
regular DisplayObjects or IUIComponents.
Usually, this method is called within setUp, and removeChild
is called from within tearDown. Using these methods, ensures that added
children will be subsequently removed, even when tests fail.
Here is an example of the addChild method:
private var instance:MyComponent;
override protected function setUp():void {
super.setUp();
instance = new MyComponent();
instance.addEventListener(Event.COMPLETE, addAsync());
addChild(instance);
}
override protected function tearDown():void {
super.tearDown();
removeChild(instance);
}
public function testParam():void {
assertEquals(34, instance.value);
}
child:DisplayObject |
DisplayObject |
| asyncOperationComplete | () | method |
| asyncOperationTimeout | () | method |
public function asyncOperationTimeout(async:AsyncOperation, duration:Number):voidParameters
async:AsyncOperation |
|
duration:Number |
| cleanUp | () | method |
protected function cleanUp():voidOverride this method in Asynchronous test cases or any other time you want to perform additional member cleanup after all test methods have run
| countTestCases | () | method |
public function countTestCases():intCounts the number of test cases executed by run(TestResult result).
Returnsint |
| createResult | () | method |
protected function createResult():TestResultCreates a default TestResult object
ReturnsTestResult |
See also
| getContext | () | method |
public function getContext():DisplayObjectContainer
Returns the visual DisplayObjectContainer that will be used by
addChild and removeChild helper methods.
DisplayObjectContainer |
| getCurrentMethod | () | method |
public function getCurrentMethod():String
Returns
String |
| getIsComplete | () | method |
public function getIsComplete():Boolean
Returns
Boolean |
| getName | () | method |
public function getName():StringGets the name of a TestCase
ReturnsString — returns a String
|
| getResult | () | method |
| getTestMethods | () | method |
public function getTestMethods():Array
Returns
Array |
| removeChild | () | method |
protected function removeChild(child:DisplayObject):DisplayObject
Helper method for removing added DisplayObjects.
Update: This method should no longer fail if the provided DisplayObject
has already been removed.
child:DisplayObject |
DisplayObject |
| run | () | method |
public function run():voidA convenience method to run this test, collecting the results with either the TestResult provided or a default, new TestResult object. Expects either: run():void // will return the newly created TestResult run(result:TestResult):TestResult // will use the TestResult that was passed in.
See also
| runBare | () | method |
public function runBare():voidRuns the bare test sequence.
— if any exception is thrown
|
| runTearDown | () | method |
protected function runTearDown():void
| setContext | () | method |
public function setContext(context:DisplayObjectContainer):voidParameters
context:DisplayObjectContainer |
| setName | () | method |
public function setName(name:String):voidSets the name of a TestCase
Parametersname:String — The name to set
|
| setResult | () | method |
| setTestMethods | () | method |
protected function setTestMethods(methodNodes:XMLList):voidParameters
methodNodes:XMLList |
| setUp | () | method |
protected function setUp():voidSets up the fixture, for example, instantiate a mock object. This method is called before each test is executed. throws Exception on error.
private var instance:MyInstance;
override protected function setUp():void {
super.setUp();
instance = new MyInstance();
addChild(instance);
}
| tearDown | () | method |
protected function tearDown():void
Tears down the fixture, for example, delete mock object.
This method is called after a test is executed - even if the test method
throws an exception or fails.
Even though the base class TestCase doesn't do anything on tearDown,
It's a good idea to call super.tearDown() in your subclasses. Many projects
wind up using some common fixtures which can often be extracted out a common project
TestCase.
tearDown is not called when we tell a test case to execute
a single test method.
— on error.
|
private var instance:MyInstance;
override protected function setUp():void {
super.setUp();
instance = new MyInstance();
addChild(instance);
}
override protected function tearDown():void {
super.tearDown();
removeChild(instance);
}
| toString | () | method |
public override function toString():StringReturns a string representation of the test case
ReturnsString |
| DEFAULT_TIMEOUT | constant |
protected static const DEFAULT_TIMEOUT:int = 1000
| PRE_SET_UP | constant |
protected static const PRE_SET_UP:int = 0
| RUN_METHOD | constant |
protected static const RUN_METHOD:int = 2
| SET_UP | constant |
protected static const SET_UP:int = 1
| TEAR_DOWN | constant |
protected static const TEAR_DOWN:int = 3
// Assume we are testing a class that looks like this:
package utils {
public class MathUtil {
// Add one to the number provided:
public function addOne(num:Number):Number {
return num + 1;
}
}
}
// This is a test case for the class above:
package utils {
import asunit.framework.TestCase;
public class MathUtilTest extends TestCase {
private var mathUtil:MathUtil;
public function MathUtilTest(methodName:String=null) {
super(methodName)
}
override protected function setUp():void {
super.setUp();
mathUtil = new MathUtil();
}
override protected function tearDown():void {
super.tearDown();
mathUtil = null;
}
public function testInstantiated():void {
assertTrue("mathUtil is MathUtil", mathUtil is MathUtil);
}
public function testAddOne():void {
assertEquals(5, mathUtil.addOne(4));
}
}
}
For the purpose of the following test example, we'll be using a Runner that
looks like this:
package {
import asunit.textui.TestRunner;
import controls.ViewComponentTest;
public class SomeProjectRunner extends TestRunner {
public function SomeProjectRunner() {
// start(clazz:Class, methodName:String, showTrace:Boolean)
// NOTE: sending a particular class and method name will
// execute setUp(), the method and NOT tearDown.
// This allows you to get visual confirmation while developing
// visual entities
start(ViewComponentTest, 'testGoToHalfSize', TestRunner.SHOW_TRACE);
}
}
}
The following Component is a visual component whose primary feature
is the 'goToHalfSize' method.
package controls {
import flash.display.Sprite;
import flash.events.Event;
public class ViewComponent extends Sprite {
private var _width:Number;
private var _height:Number;
public function ViewComponent() {
_width = 640;
_height = 480;
}
public function draw():void {
graphics.clear();
graphics.beginFill(0xFFCC00);
graphics.drawRect(0, 0, width, height);
graphics.endFill();
}
public function goToHalfSize():void {
width = Math.round(width / 2);
height = Math.round(height / 2);
draw();
}
override public function set width(width:Number):void {
_width = width;
}
override public function get width():Number {
return _width;
}
override public function set height(height:Number):void {
_height = height;
}
override public function get height():Number {
return _height;
}
}
}
And finally, the test case that will validate the ViewComponent class and
it's goToHalfSize method.
package controls {
import asunit.framework.TestCase;
public class ViewComponentTest extends TestCase {
private var instance:ViewComponent;
public function ViewComponentTest(methodName:String=null) {
super(methodName)
}
override protected function setUp():void {
super.setUp();
instance = new ViewComponent();
addChild(instance);
}
override protected function tearDown():void {
super.tearDown();
removeChild(instance);
instance = null;
}
public function testInstantiated():void {
assertTrue("instance is ViewComponent", instance is ViewComponent);
}
public function testGoToHalfSize():void {
instance.width = 400;
instance.height = 200;
instance.goToHalfSize();
assertEquals('width', 200, instance.width);
assertEquals('height', 100, instance.height);
}
}
}
Assume we have a feature that performs some asynchronous action:
package net {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.setTimeout;
[Event(name="complete", type="flash.events.Event")]
public class AsyncMethod extends EventDispatcher {
public function doSomething():void {
setTimeout(function():void {
dispatchEvent(new Event(Event.COMPLETE));
}, 100);
}
}
}
We can test this feature with the following test case:
package net {
import asunit.framework.TestCase;
import flash.events.Event;
public class AsyncMethodTest extends TestCase {
private var instance:AsyncMethod;
public function AsyncMethodTest(methodName:String=null) {
super(methodName)
}
override protected function setUp():void {
super.setUp();
instance = new AsyncMethod();
}
override protected function tearDown():void {
super.tearDown();
instance = null;
}
public function testInstantiated():void {
assertTrue("instance is AsyncMethod", instance is AsyncMethod);
}
public function testDoSomething():void {
instance.addEventListener(Event.COMPLETE, addAsync());
instance.doSomething();
}
}
}