处理客户事件和 Async.asyncHandler() 的 flexunit

发布于 2024-10-04 21:26:21 字数 713 浏览 5 评论 0原文

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

我定义了一个名为 HelloCompo(extends 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()执行时,它真的被调度了,所以出了什么问题?)

Any one know how does Async.asyncHandler() work and if Async.processOnEvent() can only used in [Before] method.(Any one know some helpful document besides http://docs.flexunit.org/).

I define a MXML component named HelloCompo(extends Vbox), and the component define a function named hello(), in the hello() dispacthed a customer event named HelloEvent(the event type just named "hello"), and in another function named init() listened for the event, I want to test whether the event is dispatched properly or not. So I have the test following:

var helloCompo = new HelloCompo();

helloCompo.hello();

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

The test will always excute the handleTimeOut method(means the HelloEvent is not dispatched, but when helloCompo.hello() excute, it really dispacthed, so what's going wrong?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慵挽 2024-10-11 21:26:21
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"));
        }
    }
}
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"));
        }
    }
}
简单 2024-10-11 21:26:21

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

I think you need to add your event listener before calling hello() actually

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文