Skip navigation
Jessica Shizuku
Currently Being Moderated

how to access stage in flexunit

Jan 10, 2011 5:11 AM

I've found in the following post (http://forums.adobe.com/message/2371850#2371850) some suggestions about this topic, but as I'm relatively new to as3, I'm still confused about how to make it work. My team has an as3 only project, and we refer to the stage very often this way:

 

package clipper{

     ...

     public function init() {

          onInit();

          ...

     }

 

     private function onInit() {

          this.stage.frameRate = 24;

          this.stage.addChild(...);

          ...

     }

}

 

Everytime when my testcases call statements like this, it'll give error saying "null object reference", etc. As far as I've read in the other post, I should initiate a VisualTestEnvironment in the test runner, and use the UIImpersonator in the test cases, am I right? If I wanna write a testcase for function init as below, how shall I modify the codes? What shall I add in the mxml test runner? Thanks. I'm building and running the tests with ant.

 

var _clip:clipper;

 

[Before]

public function setUp():void {

     _clip = new clipper();

     UIImpersonator.addChild(_clip);

}

 

[Test(async)]

public function testInit():void{

     _clip.init();

     assert...

}

 
Replies
  • Currently Being Moderated
    Jan 10, 2011 8:52 AM   in reply to Jessica Shizuku

    Not sure if you are using Flash Builder or another tool. First thing, when you are running your tests, you need to provide FlexUnit a reference to the stage:

     

    It would look something like this:

     

    core = new FlexUnitCore();

    core.visualDisplayRoot = this;

     

    /// listeners or other junk

     

    core.run( MyTests );

     

    In AS3 you can say this.stage inside of any visual class such as a movie clip or sprite that is on the stage, but only visual classes get this property. Since FlexUnit tests aren't visual, you don't get that benefit. The reason you are getting the null error is that the visualDisplayRoot property is not being set.

     

    Mike

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 29, 2012 10:12 AM   in reply to Jessica Shizuku

    I found this BDD project page, which includes a stage access example:

    Cuke4AS3 https://github.com/flashquartermaster/Cuke4AS3/

     

    [EDIT]

    After much research and complication, I ended up using a Singleton that provides a stage reference that was passed in from a `creationComplete` handler in my TestRunner.

     

    =======TestRunner.mxml==========================================

     

    <?xml version="1.0" encoding="utf-8"?>

    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

        creationComplete="onCreationComplete()"

        xmlns:flexUnitUIRunner="http://www.adobe.com/2009/flexUnitUIRunner"

        styleName="flexUnitApplication"

        layout="absolute" height="800" width="1000">

        <mx:Script>

            <![CDATA[

                import org.flexunit.runner.FlexUnitCore;

                private var core:FlexUnitCore;

                public function onCreationComplete():void {

                    core = new FlexUnitCore();

                    core.addListener( new TraceListener() );

                    core.addListener( new UIListener( uiListener ));

                    new StageLocator(systemManager.stage);

                    core.run(MyTestSuite);

                }

            ]]>

        </mx:Script>

        <mx:Style>

            Application {

                   backgroundColor: #3872b2;

                   backgroundGradientColors: #3872b2, #0c1a3d;

                   backgroundGradientAlphas: 1, 1;

                   themeColor: #ffffff;

                   color: #444444;

                   fontFamily: "Myriad Pro Semibold";

                   fontSize: 12;

                }

        </mx:Style>

        <flexUnitUIRunner:TestRunnerBase id="uiListener" width="100%" height="100%" />

    </mx:Application>

     

    =======StageLocator.as==========================================

     

    package {

    import flash.display.Stage;

    public class StageLocator {

        public static var instance:StageLocator;

        public static var stage:Stage;

        public function StageLocator ($stage:Stage) {

            instance = this;

            stage = $stage;

        }

    }

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points