views:

173

answers:

3

I need it in FlexUnit to test private methods. Is there any possibility to do this via reflection by using describeType or maybe flexUnit has some build in facility? I dislike artificial limitation that i cannot test private functions, it greatly reduces flexibility. Yes it is good design for me to test private functions, so please do not advise me to refactor my code. I do not want to break the encapsulation for the sake of unit testing.

+4  A: 

I'm 99% certain this isn't possible and I'm intrigued to know why you would want to do this.

You should be unit testing the output of a given class, based on given inputs, regardless of what happens inside the class. You really want to allow someone to be able to change the implementation details so long as it doesn't change the expected outputs (defined by the unit test).

If you test private methods, any changes to the class are going to be tightly coupled to the unit tests. if someone wants to reshuffle the code to improve readability, or make some updates to improve performance, they are going to have to update the unit tests even though the class is still functioning as it was originally designed.

I'm sure there are edge cases where testing private methods might be beneficial but I'd expect in the majority of cases it's just not needed. You don't have to break the encapsulation, just test that your method calls give correct outputs... no matter what the code does internally.

James Hay
A: 

You cannot use describeType for that.

From the Livedocs - flash.utils package:

[...]

Note: describeType() only shows public properties and methods, and will not show properties and methods that are private, package internal or in custom namespaces.

[...]

Baelnorn
+1  A: 

Just create a public method called "unitTest" and call all your unit tests within that method. Throw an error when one of them fails and call it from your test framework:

try {
   myobject.unitTest();
} catch (Exception e) {
   //etc.
}
Andir