2 Replies Latest reply: Feb 24, 2011 2:07 PM by Michael Labriola RSS

    Providing a reference to the Stage in a Pure AS3 Environment

    JonnyReeves Community Member

      Hi All,

       

      I'm having real problems trying to get a reference to the Stage in FlexUnit 4.1 TestCases.  I am using the FlexUnit Ant Tasks to generate the TestRunner.mxml file which I have modified to bootstrap the VisualTestEnvironmentBuilder like so:

       

           VisualTestEnvironmentBuilder.getInstance(stage);

       

      However, when FlexUnit tries to run each testcase, I get the following stacktrace:

       

      <testcase classname="com.moshigames.api::TestContentAPIImpl" name="loadFails_completeCallbackCalled" time="5" status="error"><error message="Error #1034: Type Coercion failed: cannot convert org.fluint.uiImpersonation.actionScript::ActionScriptVisualTestEnvironment@5671191 to mx.core.IUIComponent." type="com.moshigames.api::TestContentAPIImpl.loadFails_completeCallbackCalled" ><![CDATA[TypeError: Error #1034: Type Coercion failed: cannot convert org.fluint.uiImpersonation.actionScript::ActionScriptVisualTestEnvironment@5671191 to mx.core.IUIComponent.

      at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::addingChild()

      at mx.core::Container/addChildAt()

      at mx.core::Container/addChild()

      at org.fluint.uiImpersonation.actionScript::ActionScriptEnvironmentBuilder/buildVisualTestEn vironment()

      at org.fluint.uiImpersonation::VisualTestEnvironmentBuilder/buildVisualTestEnvironment()

      at org.flexunit.internals.runners.watcher::FrameWatcher/getStage()

       

      Am I missing something obvious here?

       

      Once possible solution to this problem would be to use a simple ServiceLocator type class for retrieving a stage reference in the Unit Test Cases; Something simple like:

       

           FlexUnitStageLocator.stage;

       

      Which could be bootsrapped as part of the TestCase setup - this would be enourmously beneficial for testing Clients which have to interace with the Stage.

       

      Thanks

      jonny.

        • 1. Re: Providing a reference to the Stage in a Pure AS3 Environment
          Michael Labriola Community Member

          You mention you are in a pure AS3 environment, but the code below says things like mx.core::Container which is Flex.

           

          So, at least 2 things

           

          1) You have the Flex version of the FlexUnit.swc not the ActionScript-only version. You mention the ant tasks, so it is possible that our ant tasks are doing something wrong and including the wrong version for you

           

          2) When it is complied it is using Flex libraries. This code would actually crash with a verify error on startup if you were using the Flex version of FlexUnit and compiling in an ASOnly environment. The fact that it runs this far means it is the FlexUnit version of the SWC and it is actually compiling in Flex classes too.

           

          The specific cast you mention is crashing because in a Flex version of the project, the visual root ends up being an IUIComponent.

           

          The ant tasks are not my specialty, but let me take a look and see what I see. The ultimate issue though is the library.

           

          Mike

          • 2. Re: Providing a reference to the Stage in a Pure AS3 Environment
            Michael Labriola Community Member

            Side note:

             

            To keep moving forward you could do exactly what you mentioned. You could just make a static class in your test project that you assign the stage reference to at startup and grab it during your tests.

             

            Obviously want to fix this for you, but just noting that is viable in the meantime.

             

            Mike