2010-11-27 6 views
3

Async.asyncHandler()がどのように動作し、Async.processOnEvent()が[Before]メソッドでのみ使用できるか知っています(いずれもhttp://docs.flexunit.org/以外にも役立つドキュメントがあります)。ハンドル顧客イベントのflexunitとAsync.asyncHandler()

HelloComompという名前のMXMLコンポーネント(Vboxを拡張)を定義し、hello()という名前の関数を定義し、HelloEventという名前の顧客イベント( "hello"という名前のイベントタイプ)を定義します。イベントを聞いたinit()という別の関数では、イベントが正しく送出されているかどうかをテストしたい。だから私は次のテストを受けています:

var helloCompo = new HelloCompo();

helloCompo.hello(); 

helloCompo.addEventListener("hello", Async.asyncHandler(this, handleHello, 1000, null, handleTimeOut)); 

テストは常にhandleTimeOut方法(HelloEventがディスパッチされていないことを意味、しかしときhelloCompo.hello()ので、何が間違っているだろう、それは本当にdispacthed、excute?)

答えて

6
package flexUnitTests 
{ 
    import flash.events.Event; 

    import org.flexunit.asserts.assertTrue; 
    import org.flexunit.asserts.fail; 
    import org.flexunit.async.Async; 

    public class HelloTest 
    {  
     private var helloCompo:HelloCompo; 

     [Before] 
     public function setUp():void 
     { 
      helloCompo = new HelloCompo(); 
     } 

     [After] 
     public function tearDown():void 
     { 
      helloCompo = null; 
     } 

     [Test(async)] 
     public function testHello():void 
     { 
      var handler:Function = Async.asyncHandler(this, helloHandler, 300, null, helloFailed); 
      helloCompo.addEventListener("hello", handler); 
      helloCompo.hello(); 
     } 

     private function helloHandler(event:Event, passThroughObject:Object):void 
     { 
      //assert somthing 
     } 

     private function helloFailed(event:Event, passThroughObject:Object):void 
     { 
      fail("hello not dispatched"); 
     } 


    } 
} 

HelloCompo.as

package 
{ 
    import flash.events.Event; 
    import flash.events.EventDispatcher; 
    import flash.events.IEventDispatcher; 

    public class HelloCompo extends EventDispatcher 
    { 
     public function HelloCompo(target:IEventDispatcher=null) 
     { 
      super(target); 
     } 

     public function hello():void 
     { 
      dispatchEvent(new Event("hello")); 
     } 
    } 
} 
2

をexcuteます私はあなたが必要だと思います)(ハロー呼び出す前にイベントリスナーを追加するために、実際に

+0

@pez。申し訳ありませんが、私はそうだとは思わない – jason

+0

うん、私の悪い、あなたは正しい。 私は簡単なテストをしました。 Testタグにasyncプロパティを設定してもよろしいですか? –

+0

私の編集した回答を参照してください –

関連する問題