Flexunit client event processing and Async.asyncHandler ()

Anyone knows how it works Async.asyncHandler(), and if it Async.processOnEvent()can only be used in the [Before] method. (Anyone knows a useful document except http://docs.flexunit.org/ ).

I define an MXML component called HelloCompo (extends Vbox), and the component defines a function called hello (), a client event called HelloEvent is sent to hello () (an event type, only called "hello"), and to another function called init () listened to the event, I want to check if the event was sent correctly. So I have a test:

var helloCompo = new HelloCompo ();

helloCompo.hello();

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

The test will always call the handleTimeOut method (means that HelloEvent has not been sent, but when helloCompo.hello () is excute, it really gets sent, so what's wrong?)

+3
source share
2 answers
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"));
        }
    }
}
+6
source

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

+2
source

Source: https://habr.com/ru/post/1777177/


All Articles