3 Replies Latest reply: May 2, 2011 1:53 PM by chrise524 RSS

    flexunit stage null reference

    chrise524 Community Member

      I am trying to test some of my AS classes in flexunit.  Some of the methods reference the stage from components that are passed to it. When I set up the flexunit testing application I use the following code:

       

      core = new FlexUnitCore();

      core.visualDisplayRoot = this;

      core.addListener( new UIListener( uiListener ));

       

      However, I still get a null reference exception whenever the stage is referenced.  I was wonding if anyone knew what is going on or what I am doing wrong.

       

      Thanks!

        • 1. Re: flexunit stage null reference
          Michael Labriola Community Member

          What is the object that 'this' refers to? Does that code exist on the main sprite, somewhere else?

           

          Glad to help just need a bit more info,

          Mike

          • 2. Re: flexunit stage null reference
            chrise524 Community Member

            The previous code is in the creationComplete event function for my test runner. (I used the flexunit 4.1 turnkey example)

             

            The "this" is the runner application.

             

            Then in the test I would have:

             

            [Test]

            public function testFunct():void

            {

                //this class stores various elements so that other classes can referance them

                Control.initApp(new Image());

             

                //this fails when testing

                Assert.assertNotNull(Control.getImageContainer().stage);

            }

             

             

             

            I am not sure why the assert equals null.

            • 3. Re: flexunit stage null reference
              Michael Labriola Community Member

              Okay, a few things.

               

              If you are using Flex, you don't need to set the visualDisplayRoot. That is for ActionScript only projects.

               

              Second, display objects in Flash don't have access to the stage until they are on the display list. So, saying

               

              new Image() won't add that item to the display list.

               

              I am not sure what Control.initApp() does, but in one of your unit tests, they way you get something on the display list is to use the UIImpersonator:

               

              UIImpersonator.addChild( someImage );

               

              After that someImage would have access to the stage. However, bigger problem. Unit tests are about isolation and if you components need to get out to the stage or if Control.initApp() somehow adds the item directly to the display list you are going to continue to have different issues as you won't be able to keep things in isolation to test.

               

              Hope that clarifies a little bit,

              Mike