3 Replies Latest reply on Oct 15, 2010 1:21 PM by sanggpark

    Flex Unit failing to handle Async events

    sanggpark Level 1

      I have following class

      package

      {

           import flash.events.Event;

           import flash.events.EventDispatcher;

       

           import mx.binding.utils.ChangeWatcher;

       

           public class TestPresentationModel extends EventDispatcher

           {

                private var _index:int = 0;

       

                [Bindable(event="indexChanged")]

                public function set index(value:int):void

                {

                     if (_index != value) {

                          _index = value;

                          dispatchEvent(new Event("indexChanged"));

                         }

                }

       

                public function get index():int

                {

                     return _index;

                }

       

       

                public function TestPresentationModel()

                {

                     ChangeWatcher.watch(this, "index", function(event:*):void {

                          if (index == 1) {

                               dispatchEvent(new Event("indexOne"));

                          }

                     });

                }

           }

      }

       

       

      and following test case

      [Test(async)]

      public function testIndex():void

      {

           var pm:TestPresentationModel = new TestPresentationModel();

       

           pm.index = 0;

           Async.failOnEvent(this, pm, "indexOne");

           Async.handleEvent(this, pm, "RandomEvent", handleRandomEvent);

           pm.index = 2;

      }

      private function handleRandomEvent(event:Event, data:Object):void

      {

           Assert.fail();

      }

       

      For some reason this test is passing. "RandomEvent" never got fired so Async.handleEvent should've timeout. Even if "RandomEvent" was fired, it should've failed with Assret.fail(); However it's passing.... why is this?

        • 1. Re: Flex Unit failing to handle Async events
          sanggpark Level 1

          I changed the ChangeWatcher to

          ChangeWatcher.watch(this, "index", function(event:*):void {

          if (index == 1) {

          dispatchEvent(new Event("indexOne"));

          } else if (index == 2) {

          dispatchEvent(new Event("RandomEvent"));

          }

          });

          which got me "Error: Asynchronous Event Received out of Order"

          This seems wrong as well. This sounds like to me Async was first expecting "indexOne" event then "RandomEvent" which makes me feel like once I have Async.failOnEvent(), I can have any more Async handlers afterwards.

          • 2. Re: Flex Unit failing to handle Async events
            mlabriola Level 4

            I will get you more info on this later to explain why, however:

             

            Change your code to look like this:

             

            Async.registerFailureEvent( this, pm, "indexOne" );

            //Async.failOnEvent(this, pm, "indexOne");

            Async.handleEvent(this, pm, "RandomEvent", handleRandomEvent);

            This is what you are actually after... promise more details when I have time to type, just wanted to get you an answer fast.
            Mike

            1 person found this helpful
            • 3. Re: Flex Unit failing to handle Async events
              sanggpark Level 1

              Should I be using Async.registerFailureEvent for all of them? or just in the cases like this one?