8 Replies Latest reply: Aug 30, 2011 1:41 PM by Michael Labriola RSS

    Testing Application Components

    bucpatr1 Community Member

      I'm not sure if you are aware, but it is currently very difficult to test spark Application components using FlexUnit and the UIImpersonator. An application component can't simply be added to UIImpersonator with addChild; there is already an existing Application for the test environment. What you have to do instead is create and add a SWFLoader and then use that to load an instance of the Application to test. The workaround is not very difficult, but having a mention of this issue in the documentation somewhere would have been helpful. Better yet perhaps add a function to UIImpersonator that takes care of the extra steps involved in testing an Application. What do you think?

        • 1. Re: Testing Application Components
          Michael Labriola Community Member

          Which version of FlexUnit are you using?

           

          As of 4.1 we allowed the interface to have an addElement() which accepts spark components just like the Spark Application?

           

          Mike

          • 2. Re: Testing Application Components
            bucpatr1 Community Member

            I might be running an older version since we're still using flashbuilder 4. Is there any way to update the flexunit libraries that flashbuilder uses?

            • 3. Re: Testing Application Components
              Michael Labriola Community Member

              Its a little convoluted in Flash Builder 4, so what I would suggest first instead is that you just add the FlexUnit 4.1 swc to your library path of your test project. It should trump the existing version and let you test with 4.1 before you have to hunt down the existing swc, etc.

               

              Mike

              • 4. Re: Testing Application Components
                bucpatr1 Community Member

                No luck there. Unfortnately, we are displaying our tests using org.flexunit.flexui.TestRunnerBase.mxml which is based on a flex3 component. When I try to run my tests it's throwing an error saying that addElement can't be used in non-flex4 projects.

                • 5. Re: Testing Application Components
                  Michael Labriola Community Member

                  Its okay that it is based on flex 3 components, it would just need to be compiled with the Flex 4 SDK or you need to wait until the next release of FlexUnit to make it cleaner.

                   

                  In the 4.1 release we basically look for global clues as to whether it is 4.x or 3.x as it is unfortunately difficult (as you have seen) to handle them both together.

                   

                  Mike

                  • 6. Re: Testing Application Components
                    Michael Labriola Community Member

                    I will see if we can accelerate any of this in a dot release.

                    • 7. Re: Testing Application Components
                      bucpatr1 Community Member

                      So addElement() is not usable in the current release version? My project is compiled with flex 4.5 so I don;t think thats the issue here.

                       

                      Also, even if addElement did work, it would still be better to use the SWFLoader method, otherwise the applicationComplete event would never get dispatched.

                      • 8. Re: Testing Application Components
                        Michael Labriola Community Member

                        Yes, it is completely usable.

                         

                        How it works is that FlexUnit tries to see if there is a mx.core.Container linked into your project. If there is, it uses that as the basis for the UIImpersonator. If there isn't one, then it uses spark.components.Group.

                         

                        It does this because it is possible to compile in Flex 4.x with or without the MX components.

                         

                        If your project has both, MX and Spark, then you can continue to use addChild() and you should be able to add either spark or mx components to the display list with 4.1.  If your project is spark only then you need to use addElement(). We are just trying to make this cleaner for future releases but it does work now.

                         

                        As far as the loader, etc. I certainly understand why you are doing that and if it works then by all means its a good solution. FlexUnit was never intended though to be able to test full applications, so things like applicationComplete were never on our radar. So, for now, you will need to continue your work around to have that functionality or, you could always manually dispatch it (myApp.dispatchEvent( new Event("applicationComplete") ) ) at the appropriate time.

                         

                        HTH,

                        Mike