2010-08-11 70 views
2

FlexUnit wiki中,我讀到了由Fluint人員貢獻的非常有趣的SequenceRunner。執行運行的SequenceRunner實例()方法時,我現在想運行一個測試,這是非常類似的例子,不過,我得到這個異常:如何在FlexUnit中使用SequenceRunner 4

Cannot add asynchronous functionality to methods defined by Test,Before or After that are not marked async 
Error: Cannot add asynchronous functionality to methods defined by Test,Before or After that are not marked async 
    at org.flexunit.async::AsyncLocator$/getCallableForTest()[C:\Users\dmoor e\Documents\_Production\Flex Unit 4\GIT\FlexUnit4\src\org\flexunit\async\AsyncLocator.as:82] 
    at org.fluint.sequence::SequenceWaiter/setupListeners()[C:\Users\dmoore\ Documents\_Production\Flex Unit 4\GIT\FlexUnit4\src\org\fluint\sequence\SequenceWaiter.as:100] 
    at org.fluint.sequence::SequenceRunner/continueSequence()[C:\Users\dmoor e\Documents\_Production\Flex Unit 4\GIT\FlexUnit4\src\org\fluint\sequence\SequenceRunner.as:177] 
    at org.fluint.sequence::SequenceRunner/run()[C:\Users\dmoore\Documents\_ Production\Flex Unit 4\GIT\FlexUnit4\src\org\fluint\sequence\SequenceRunner.as:124] 

有沒有人使用的SequenceRunner與FlexUnit 4中已經。 [Test(async)]註釋已經存在。

回答

0

感謝Michael Labriola,他在Adobe Forum對我的問題做出了迴應,我終於能夠使其運行。請注意,SequenceRunner in the Wiki的文檔已過時並部分錯誤。

  1. 不從任何TestCase類繼承。
  2. 跳過setUp中的CREATION_COMPLETE的asyncHandler。在等待組件CREATION_COMPLETE事件的測試中增加一個SequenceWaiter
  3. 測試必須標記爲異步測試,因此請將[Test(async)]元數據添加到使用SequenceRunner的測試用例。
1

這是一個完整的,非常簡單的示例測試用例類。

package test 
{ 
    import flash.events.Event; 

    import org.flexunit.asserts.assertEquals; 

    import org.fluint.sequence.SequenceRunner; 
    import org.fluint.sequence.SequenceWaiter; 

    public class test_case 
    { 
     [Test(async)] 
     public function test_function():void 
     { 
      var counter:Object = { count: 0} 
      var sr:SequenceRunner = new SequenceRunner(this); 
      sr.addStep(new SequenceWaiter(new TestWaiterTarget(counter), "myEvent", 50000)); 
      sr.addStep(new SequenceWaiter(new TestWaiterTarget(counter), "myEvent", 5000)) 
      sr.addAssertHandler(test_function_handler, counter); 
      sr.run(); 
     } 

     private function test_function_handler(event:Event, passthroughData:*):void 
     { 
      assertEquals(passthroughData.count, 2); 
     } 
    } 
} 

import flash.events.Event; 
import flash.events.EventDispatcher; 
import flash.events.TimerEvent; 
import flash.utils.Timer; 

class TestWaiterTarget extends EventDispatcher 
{ 
    var timer:Timer = new Timer(250, 1); 

    private var _counter:Object; 

    public function TestWaiterTarget(counter) 
    { 
     _counter = counter; 
     timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_timerCompleteHandler); 
     timer.start(); 
    } 

    private function timer_timerCompleteHandler(event:TimerEvent):void 
    { 
     _counter.count++; 
     dispatchEvent(new Event("myEvent")); 
    } 
}