Problem
How to create a FlexUnit TestCase class that can be used to test code.
怎么创建能用于测试代码的flex单元测试用例
Solution
Create a class that extends TestCase and include one or more public methods whose name starts with test.
创建一个类继承TestCase,类中包括一或者多个以test开头的公共的方法
Detailed explanation
1. Create an ActionScript class that extends TestCase
Create a new ActionScript class that extends TestCase. The remainder of this recipe will use the following RegExpTest as the example:
package { import flexunit.framework.TestCase; public class RegExpTest extends TestCase { } }
2. Create a method whose name starts with test
Add the following method to RegExpTest:
public function testRegExp():void { }
3. Make one or more assertions
- assertEquals: Compare with ==
- assertTrue: Check condition is true
- assertNull: Check if null
- assertStrictlyEquals: Compare with ===
FlexUnit also provides various convenience assertions which test for the opposite condition such as assertFalse and assertNotNull. See the Assert API documentation included with FlexUnit for a complete list of assertions.
Each assertion function can take an optional String as the first argument which will be included in the assertion output should the assertion fail. This String will be prefixed before the default "expected X but was Y" failure message.
When writing assertions keep in mind that if an assertion fails, the rest of the test method will not be executed.
TestCase extends Assert which defines all of the assert functions. This allows subclasses of TestCase to directly call the assert functions. The following code demonstrate the various assertion methods and should be added to the testRegExp function:
var regExp:RegExp = new RegExp("a", "i"); assertFalse(regExp.test("b")); assertFalse("regExp doesn't match", regExp.test("b")); assertNull(regExp.exec("b")); assertNull("regExp doesn't match", regExp.exec("b")); assertNotNull(regExp.exec("Apple")); assertNotNull("regExp matches", regExp.exec("Apple")); assertTrue(regExp.exec("Apple") is Array); assertTrue("regExp exec returned an Array", regExp.exec("Apple") is Array); assertEquals("A", regExp.exec("Apple")[0]); assertEquals("regExp matched A in Apple", "A", regExp.exec("Apple")[0]); assertStrictlyEquals(regExp, regExp); assertStrictlyEquals("regExp object identity", regExp, regExp);
4. Add additional test methods
Add new test methods to the TestCase to test other logical groups of operations. It is convention that each test method focus on testing a specific operation or task. For example when testing create, retrieve, update, and delete operations each one should be put into its own test method such as testCreate, testRetrieve, etc. This way should assertions start to fail, multiple failures will be reported helping diagnosis the issue.
It is important to keep in mind that the order that the test methods in a TestCase are run is random. Each test should create its own data and make no assumptions about another test having already run.
5. Add the TestCase to the TestSuite
The last step is to add the newly created TestCase to the TestSuite.
See the Adding a TestCase to a TestSuite recipe.
Reference
The final ActionScript file:
package { import flexunit.framework.TestCase; public class RegExpTest extends TestCase { public function testRegExp():void { var regExp:RegExp = new RegExp("a", "i"); assertFalse(regExp.test("b")); assertFalse("regExp doesn't match", regExp.test("b")); assertNull(regExp.exec("b")); assertNull("regExp doesn't match", regExp.exec("b")); assertNotNull(regExp.exec("Apple")); assertNotNull("regExp matches", regExp.exec("Apple")); assertTrue(regExp.exec("Apple") is Array); assertTrue("regExp exec returned an Array", regExp.exec("Apple") is Array); assertEquals("A", regExp.exec("Apple")[0]); assertEquals("regExp matched A in Apple", "A", regExp.exec("Apple")[0]); assertStrictlyEquals(regExp, regExp); assertStrictlyEquals("regExp object identity", regExp, regExp); } } }