5 Replies Latest reply on May 10, 2011 8:27 AM by brazentongue

    unit testing

    brazentongue

      I'm attempting to unit test some actionscript code, but there are conditional statements based

      upon FlashVars values.

       

      switch(FlashVars.someVariable)

      {

           case 0: // Do something testable

           case 1: // Do something else testable

      }

       

      Since I can't find a way to manipulate the FlashVars variables from the code this means that I am stuck being able to test only one code path unless I want to manually edit the HTML wrapper (not feasible). How would you overcome this? (FYI, I am new to Flex/AS as well as unit testing)

        • 1. Re: unit testing
          bucpatr1 Level 2

          The answer to your question depends on what aspect of your application this test is trying to verify. Are you testing the transfer of information from the html page into your app (ie. did all the FlashVars get imported and do they contain the expected data) or are you testing the behavior of a function or componet under certain condidtions ( ie. if flashvars.color==X was the result of some function Y )?

          • 2. Re: unit testing
            brazentongue Level 1

            No, I am not testing FlashVars, I am testing the behavior of a function whose logic depends on some flashvars variable set in the HTML (or in this case .aspx) wrapper. Please see previous example. With regular application variables, one can alter them in the unit test code to achieve the result, but it appears that flashvars are read-only from actionscript. Is there a way to alter the flashvars values with actionscript? Does flexunit allow multiple wrappers i.e. when you run this test suite use html template A, when you run that test suite use html template B?

            • 3. Re: unit testing
              UbuntuPenguin Level 4

              It sounds like you need to create a "seam" in your application.  This seam would separate the acquisition of flashVars from the class that uses them.  That way you can mock it out (assuming you are using an interface).

              1 person found this helpful
              • 4. Re: unit testing
                bucpatr1 Level 2

                In that case my suggestion would be to modify your function to take an Object as a parameter and pass in the FlashVars that way. Or you could simply assign the value from FlashVar to a public var in your Application class. Either way you create a 'seam' where you can replace the real value of FlashVar with a mock object that simulates the conditions you want to test for.

                1 person found this helpful
                • 5. Re: unit testing
                  brazentongue Level 1

                  Thanks guys these suggestions would work. Unfortunately, my position is that I am unit testing somebody else's code with instruction not to modify the methods under test....and the author did not incorporate interfaces or loose coupling best practices. This is why you don't UT somebody else's code. Make them suffer for their own mistakes!