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

标签 apache-flex flexunit flexunit4

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

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

var helloCompo = new HelloCompo();

helloCompo.hello();

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

测试总是会执行handleTimeOut方法(意思是HelloEvent没有被dispatch,但是当helloCompo.hello()执行的时候真的dispatched了,怎么回事?)

最佳答案

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"));
        }
    }
}

关于apache-flex - 处理客户事件的flexunit和Async.asyncHandler(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4291763/

相关文章:

apache-flex - 傻瓜学习 Flex 的路线图

xml - 使用 DOM 函数在 Flex (AS3) 中创建 XML 字符串

apache-flex - 如何在库中包含单元测试的最佳实践

java - 在 FlexUnit4 集成测试之间将数据库重置为已知状态的最佳方法?

apache-flex - 灵活单元 4 -- Hello World

html - 如果设置了片段标识符,则在 IE 中替换页面标题

apache-flex - Flex 4.5 - Spark DropDownList - DropDown 的最小宽度是 anchor 的宽度

apache-flex - AsUnit 与 FlexUnit – 哪个是 "better"?

java - 运行 Flexunit 测试时 BlazeDS 错误 : Detected duplicate HTTP-based FlexSessions