Installing FlexUnit is pretty straight forward. You get the Flex Unit source files, set up a Flex project, and in the process, you specify that the project should use the flexunit.swc file:
Note: If you need to add the SWC file to an existing project, right-click your project folder, select properties from the pop-up list and continue from step 5 above.
You're almost there. First I am going to briefly describe the three files that you are about to create:
Here is the MXML that belongs in the main.mxml file. Open up the file and copy and paste this in:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexunit="flexunit.flexui.*"
creationComplete="onCreationComplete()"
>
<mx:Script>
<![CDATA[
private function onCreationComplete():void
{
testRunner.test = AccountTest.suite();
testRunner.startTest();
}
]]>
</mx:Script>
<flexunit:TestRunnerBase id="testRunner" width="100%" height="100%" />
</mx:Application>
There are a few things to notice:
We defined a new xml namespace in the Application tag:
xmlns:flexunit="flexunit.flexui.*"
If you have created your own components in Flex before, this will be familiar to you. If not then I will briefly explain.
This creates a namespace called "flexunit," which refers to components located in the flexunit.flexui package. We have access to this package of course, because you associated the flexunit.swc file with the project library earlier.
To instantiate an instance of the component in MXML, you can use code similar to the following:
<flexunit:TestRunnerBase id="testRunner" />
If you had declared your namespace as "bananas", you would instantiate your component using code similar to:
<bananas:TestRunnerBase id="testRunner" />
But you've used common sense and gone for a more descriptive name.
The TestRunnerBase component is TestRunner (see the "Understanding Terminology" section, above, for a refresher). I assigned it an id of testRunner.
In the Application tag you also have the creationComplete event handler, which calls onCreationComplete(). This method sets the test suite(s) you are going to use, and then calls the startTest() method of the TestRunner to put everything in to action.
Finally, underneath the script block you instantiate our TestRunner as previously described, with a width and height of 100%.
Okay, that's it. You'll be glad to hear that you don't really need to touch this file again in this tutorial.
Create a new ActionScript file in the main project folder (at the same level as main.xml) called AccountTest.as, and paste the following code into it:
package
{
import flexunit.framework.TestCase;
import flexunit.framework.TestSuite;
public class AccountTest extends TestCase
{
public function AccountTest(methodName : String){
super(methodName);
}
public static function suite():TestSuite{
var accountTS:TestSuite = new TestSuite();
//tests are added to the suite here
return accountTS;
}
}
}
There are a few things to notice:
Finally, you must create the Account class. Create a new ActionScript file in the main project folder (at the same level as main.xml and AccountTest.as) called Account.as and paste the following code into it:
package
{
public class Account
{
public function Account(){
}
}
}
Not much to say about this. It is the bare bones of the actual class.