如何在FlexUnit 4中使用SequenceRunner

时间:2010-08-11 13:04:04

标签: flex flexunit flexunit4

FlexUnit wiki中,我读到了由Fluint人员提供的非常有趣的SequenceRunner。现在我尝试运行一个与示例非常相似的测试,但是当执行SequenceRunner实例的run()方法时,我得到了这个异常:

    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)]注释已经存在。

2 个答案:

答案 0 :(得分: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"));
    }
}

答案 1 :(得分:0)

感谢Michael Labriola,他在Adobe Forum回答了我的问题,我终于能够让它运行起来了。请注意,SequenceRunner in the Wiki的文档已过时且部分错误。

  1. 不要从任何TestCase类继承。
  2. 在setUp中跳过CREATION_COMPLETE的asyncHandler。而是在测试中添加SequenceWaiter,等待组件的CREATION_COMPLETE事件
  3. 必须将测试标记为异步测试,因此将[Test(async)]元数据添加到使用SequenceRunner的测试用例。