3

任何人都知道如何Async.asyncHandler()工作以及是否Async.processOnEvent()只能在 [Before] 方法中使用。(除了http://docs.flexunit.org/之外,任何人都知道一些有用的文档)。

我定义了一个名为 HelloCompo(extends Vbox) 的 MXML 组件,该组件定义了一个名为 hello() 的函数,在 hello() 中分发了一个名为 HelloEvent 的客户事件(事件类型刚刚命名为“hello”),并在另一个名为init() 监听事件,我想测试事件是否被正确调度。所以我有以下测试:

var helloCompo = 新的 HelloCompo();

helloCompo.hello();

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

测试总是会执行handleTimeOut方法(意味着HelloEvent没有被调度,但是当helloCompo.hello()执行时,它真的被调度了,所以出了什么问题?)

4

2 回答 2

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"));
        }
    }
}
于 2010-11-28T13:26:53.963 回答
2

我认为您需要在实际调用 hello() 之前添加事件侦听器

于 2010-11-27T13:42:43.650 回答