前些日子试用了FlexUnit4,发现真的非常的不错,非常的简单,省去了很多的麻烦。现在补个例子,加深理解。
首先你要在你的库中添加FlexUnit4.swc & FlexUnit4UIListener.swc,这两个文件你可以到
现在建立一个要测试的类
package commond { public class SimpleMath { public function add(n1:Number,n2:Number):Number { return n1 + n2; } public function divide(n1:Number,n2:Number):Number { if(n2 == 0) { throw new TypeError("sdfdasdfasd"); } return n1/n2; } } }
然后建立一个测试类
package unit { import commond.SimpleMath; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.soap.WebService; import org.flexunit.Assert; public class SimpleMathTest { private static var simpleMath:SimpleMath; [BeforeClass] public static function init():void { simpleMath = new SimpleMath(); } [AfterClass] public static function runAfterClass():void { // run for one time after all test cases } [Test] public function add():void { Assert.assertEquals(12,simpleMath.add(1,11)); } [Test(expects="TypeError")] public function divide():void { simpleMath.divide(10,0); } [Ignore("Not Ready to Run")] [Test(description="Maybe it's wright")] public function add2():void { Assert.assertEquals(12,simpleMath.add(1,1111)); } [Test(async="true")] public function asny():void { var webService:WebService = new WebService(); webService.wsdl = "http://feeds.adobe.com/webservices/mxna2.cfc?wsdl"; webService.addEventListener(FaultEvent.FAULT,onFalut); webService.addEventListener(ResultEvent.RESULT,onResult); webService.loadWSDL(); webService.getCategories(); } private function onFalut(event:FaultEvent):void { trace("onFalut"); } private function onResult(event:ResultEvent):void { trace("onResult"); } } }