2 Replies Latest reply on Dec 26, 2010 2:38 AM by Atarsh

    Testing singletons


      Hi again,

      in most cases I can test protected methods by making a test case extend the tested class.

      we use an internal class to prevent creating instances of our singleton classes outside the singleton class itself, and as far as singletons are concerned this is wonderful.

      the problem, however, arises when trying to test the singleton's protected methods - since nothing outside the singleton class knows the inner Enforcer class, the singleton cannot be extended.

      one way of solving this problem is to make all the singleton's methods public - this will allow testing, but it is wrong with regards to software design.

      is there any other way I can test my protected singleton methods?

      otherwise, if anyone knows another way of creating singletons in AS3 that will allow testing, I'm not very fanatic about the one mentioned above and I'd love to hear about it.





        • 1. Re: Testing singletons
          nikos101 Level 2

          Don't make them public, create public functions in your singleton enforcer that access those protected methods.

          • 2. Re: Testing singletons
            Atarsh Level 1

            the enforcer is not accessible from anywhere outside the singleton class, so letting it have public methods doesn't make sense. or maybe I didn't understand?

            I eventually used conditional compilation - one constructor (and getInstance()) for testing builds, another one for regular builds. not the safest way, but since the constructor and getInstance are prettsy simple code pieces, I believe this is good  enough.

            thanks, anyway