flex Test Driven Development


Listing 1. Przypadek testowy
public class SampleTest extends TestCase //przypadek testowy
{
public function SampleTest(methodName : String = null) {
super(methodName);
}
public static function suite():TestSuite {
var sampleTS:TestSuite = new TestSuite(); // zestaw testowy
sampleTS.addTestSuite(SampleTest);
accountTS.addTest(new SampleTest("testA"));
return sampleTS;
}
public function testA():void { // metoda testowa
var sample:Sample = new Sample();
assertEquals("Expecting zero", 0, sample.getBalance()); // asercja
}

override public function setUp():void {
}

override public function tearDown():void {
}
}



Listing 2. Operator testu

xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexunit="flexunit.flexui.*"
creationComplete="onCreationComplete()">

private function onCreationComplete():void
{
testRunner.test =CartTest.suite();
testRunner.startTest();
}
]]>

// operator testu


Listing 3. Klasa zawierajca przypadek testowy
package
{
import flexunit.framework.TestCase;
import flexunit.framework.TestSuite;

public class CartTest extends TestCase
{
public function CartTest(methodName : String){
super(methodName);
}

public static function suite():TestSuite{
var cartTS:TestSuite = new TestSuite();
//tutaj umiecisz testy
return cartTS;
}
}
}

Listing 4. Klasa, któr bdziemy testowa
package
{
public class Cart
{
public function Cart(){
}
}
}

Listing 5. Test nr 1
public function testNew():void{
var cart:Cart = new Cart();
assertEquals("Nic nie powinno by w koszyku", 0, cart.getQuantity());
}

Listing 6. Metoda suite()
public static function suite():TestSuite{
var cartTS:TestSuite = new TestSuite();
cartTS.addTest(new CartTest("testNew"));

return cartTS;
}


Listing 7. Piszemy metod w klasie Cart
package
{
public class Cart
{
private var _quantity:Number;
public function Cart(){
}
public function getQuantity():Number{
return _quantity;
}
}
}

Listing 8. Test iloci przedmiotów w koszyku
public function testAddNewItem():void{
var cart:Cart = new Cart();
var newItem:Object = {id:731, name:"Ksika", price:26.80};
cart.addItem(newItem);
assertEquals("Powinien by jeden przedmiot", 1, cart.getQuantity());
}

Listing 9. Dodajemy nowy test
public static function suite():TestSuite{
var cartTS:TestSuite = new TestSuite();
cartTS.addTest(new CartTest("testNew"));
cartTS.addTest(new CartTest("testAddNewItem"));
return cartTS;
}

Listing 10. Wprowadzamy zmiany do pliku Cart.as
package
{
public class Cart
{
private var CartItems:Array = new Array();
public function Cart(){
}
public function getQuantity():Number{
return CartItems.length;
}
public function addItem(item:Object):void{
CartItems.push(item);
}
}
}

Listing 11. Testujemy i wprowadzamy nowe funkcjonalnoci
public function testAddItemAndCalc():void{
var cart:Cart = new Cart();
var newItem:Object = {id:731, name:"Ksika", price:26.80};
cart.addItem(newItem);
newItem = {id:945, name:"Pyta", price:32.50};
cart.addItem(newItem);
assertEquals("Powinienny by dwa przedmioty", 2, cart.getQuantity());
assertEquals("Warto towarów w koszyku", 59.30, cart.getValue());
}

Listing 12. Dodajemy kolejn metod do klasy Cart
public function getValue():Number{
var val:Number = 0;
for(var i:int = 0; i val += CartItems[i].price;
}
return val;
}



Listing 13. Test oblicze rabatu
public function testPriceWithDiscount():void{
var cart:Cart = new Cart();
var newItem:Object = {id:731, name:"Ksika", price:26.80};
cart.addItem(newItem);
newItem = {id:945, name:"Pyta", price:32.50};
cart.addItem(newItem);
assertEquals("Warto towarów po udzieleniu rabatu 15%", 50.41, cart.getDiscountValue(15));
}

Listing 14. metoda getDiscountValue() obliczy cen towarów po rabacie i zwróci jej warto
public function getDiscountValue(discount:Number):Number{
var val:Number = getValue();
val -= val * (discount/100);
return val;
}

Listing 15. Szlifujemy metod, tak by zaokrglaa cen do penych groszy
public function getDiscountValue(discount:Number):Number{
var val:Number = getValue();
var roundVal:Number = 0;

val -= val * (discount/100);
roundVal = Math.round(val * 100)/100;

return roundVal;
}


Wyszukiwarka

Podobne podstrony:
Developing Your Intuition With Distant Reiki And Muscle Test
klucz test zawodowy Y6ZUUDOV
Test dla kierowcy[1]
candi self test
2007 01 Web Building the Aptana Free Developer Environment for Ajax
pytania2009cz1 test
MaturaSolutionsAdv Unit 4 Progress test B
Test II III etap VIII OWoUE
przykładowy test A
Unit 7 Progress test B
developer
1 Test Starożytna Grecja gr1 lic
OTWP 2010 TEST III

więcej podobnych podstron