4 Replies Latest reply on Oct 28, 2009 1:44 PM by beaufour

    Testing for list of async events

    beaufour

      Hi,

       

      I've been looking into using flexunit 4 for testing and would like test an object that will emit a chain/list of async events, in a particular order. Doing this:

      ---

      [Test(async, timeout="10000")]
      public function foo():void {
        Async.handleEvent(this, obj, "com.foo.event1", event1Handler);

        obj.kickoff();

      }

       

      private event1Handler(ev:Event, data:*):void {

        Assert.assertTrue(something);

        // What do to from here?

      }

      ---

       

      Tests that first event, but how do I setup the test to test that I get for example the events "com.foo.event2" and "com.foo.event3" in that order, and with specific asserts in each step?

       

      Thanks,

       

      Allan

        • 1. Re: Testing for list of async events
          mlabriola Level 4

          In your example, if you added the next Async.handleEvent inside of your event1Handler, it would work as you are expecting.

           

          Mike

          • 2. Re: Testing for list of async events
            beaufour Level 1

            It does not quite work for me though. Here's a larger example:

            ---

            [Test(async, timeout="90000")]
            public function foo():void {
              Async.handleEvent(this, obj, "com.foo.event1", event1Handler);

              Async.handleEvent(this, obj, "com.foo.event2", event2Handler);

              Async.handleEvent(this, obj, "com.foo.event3", event3Handler);

             

              obj.kickoff();

            }

             

            ...

             

            private event3Handler(ev:Event, data:*):void {

              Assert.assertTrue(something);

             

              Async.handleEvent(this, obj, "com.foo.event4", event4Handler);

              Async.handleEvent(this, obj, "com.foo.event5", event5Handler);

            }

             

            ...

             

            ---

             

            The above "works" for me for checking the three first events, which are fired rapidly after the .kickoff() call. I also hit the event3Handler() function, because the test fails if a set 'something' to false. But the test completes succesfully before event4 and event5 are fired. These can be fired up to 30 seconds after the event3 event. So it seems it pretty much ignores those two last .handleEvent calls?

             

            ... Allan

            • 3. Re: Testing for list of async events
              mlabriola Level 4

              If you can send me something reproducible I will be glad to take a look. (even if you just set it up with timer events for now)

               

              The example you have but together should work fine. event3handler should not cause the test to end as the additional Async statements will prolong the test context.

               

              Mike

              • 4. Re: Testing for list of async events
                beaufour Level 1

                Aha, got it. I apparently had misunderstood the [Test(timeout="90000")] ... I assumed it was the overall timeout for the test execution, but each handleEvent() defaults to 500, so if I do this:

                 

                --

                [Test(async, timeout="90000")]
                public function foo():void {
                  Async.handleEvent(this, obj, "com.foo.event1", event1Handler, 90000);

                  Async.handleEvent(this, obj, "com.foo.event2", event2Handler, 90000);

                  Async.handleEvent(this, obj, "com.foo.event3", event3Handler, 90000);

                  Async.handleEvent(this, obj, "com.foo.event4", event4Handler, 90000);

                  Async.handleEvent(this, obj, "com.foo.event5", event5Handler, 90000);

                 

                  obj.kickoff();

                }

                --

                 

                I'm back in business

                 

                ... Allan