I'm creating an object pool in AS3 that uses a stack as its base data structure (implemented with a linked list). I'm new to FlexUnit, but I'd really like to have some unit tests for my classes (as testing them in a new project would be cumbersome at best).
I've run into some problems trying to track variables, though, as my class members aren't public; I want to be able to test my private methods that only affect members of the class. (Examples include creation of the list, popping a node off of the stack, pushing a node back onto the stack, etc.)
FlexUnit's practices seem to state that you have a separate test class that runs test methods against your classes. However, these classes won't have access to my private variables, and I don't want to have to create getters and setters just for the unit tests. Is there a way around this? Can test methods be added inside the class itself, Python-style, instead of in a test case class?
I apologize if this has been asked before. I'm new to this; I appreciate your help. Let me know if I need to clarify with code snippets or anything.
Edit: I've realized that my data structure is actually a stack, not just a generic linked list. I've updated the question to reflect this.
Here's a trick I use with the modern unit testing frameworks. With the modern frameworks, you don't have to extend a TestCase-class or a TestSuite-class. This basic thing gives you such a freedom when you have to deal with testing methods or class fields which aren't public.
Here's the trick:
Let's assume this is the class you need to test:
Your test can inherit the Stack:
If you don't like
protected
you can also usepackage
-access, but make sure the test and the class under test are in the same package.It's important not to allow your abstraction to leak with getters and setters methods and public field and methods solely for the purpose of testing. On the other hand if the purpose of your class is to be a data structure then getters and setters make perfect sense. Robert C. Martin discusses in detail the dichotomy between data structures and objects.