apache-flexflexunitflexunit4

flexunit of handle customer event and Async.asyncHandler()


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?)


Solution

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