
499 CHAPTER 23 Debugging and testing
ts.addTest( new myTest( "testMilesToKm" ) );
ts.addTest( new myTest( "testKmToMiles" ) );
return ts;
}
public function testMilesToKm():void {
var mph:Number = 65;
var kmh:Number = speedConverter.toKmh( mph );
assertTrue( "We want to see 104km/h", kmh == 104 );
}
public function testKmToMiles():void {
var kmh:Number =104;
var mph:Number = speedConverter.toMph( kmh );
assertTrue( "We want to see 65mph", mph==65 );
}
}
}
This code tests an ActionScript class called speedConverter (see listing 23.9), whose
purpose is to convert speed values from km/h to mph and vice versa.
package
{
public class speedConverter
{
public static function toKmh(i:Number):Number
{
return (i*1.6);
}
public static function toMph(i:Number):Number
{
return (i/1.6);
}
}
}
The test runner application has been created, and the test
cases are ready to go. All that’s left is to execute them.
EXECUTING THE TESTS
A number of files are involved, so let’s recap by looking at
figure 23.7.
The test runner won’t run automatically because it’s not the
project’s main
MXML file. Don’t fret! You can easily work
around this by loading the TestRunner.mxml file, then click-
ing the Run/Launch application button.
In this example, you begin the tests with the click of a button.
Listing 23.9 speedConverter.as: the test suite tests this code
Figure 23.7 Files
involved in the FlexUnit
test case
Kommentare zu diesen Handbüchern