flexunit of handle customer event和Async.asyncHandler()

时间:2010-11-27 13:00:56

标签: flex flexunit flexunit4

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

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

var helloCompo = new HelloCompo();

helloCompo.hello();

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

测试将始终执行handleTimeOut方法(意味着不调度HelloEvent,但是当helloCompo.hello()执行时,它真的发生了干扰,那么出了什么问题?)

2 个答案:

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

答案 1 :(得分:2)

我认为你需要在调用hello()之前添加你的事件监听器