Skip to main content
Participant
November 9, 2009
Answered

How do I test a private function with FlexUnit4

  • November 9, 2009
  • 3 replies
  • 1304 views

Hi,


How do I (If it's possible) test a private function of a class i want to test.


I really don't want to change the function to public only for testing it.


Any suggestions

This topic has been closed for replies.
Correct answer mlabriola

will this work, yes.

is it the best way, probably. that or you could /ake extensions of your classes with public methods that call the protected ones and test those. however, there isnt much more we can give you on this. testing private member functions is not the goal in my mind so i dont have a lot of additional thoughts on doing it.

mike

3 replies

gavriguyAuthor
Participant
November 12, 2009

Thanks for the answers,

But my question is more general.

I wanted to know if there is a way to do that.

I could change the privete function to protected but then i'll
have to make the testcase as an extension of the tested class.

I know in flexunit4 you don't have to extend the testcase calss. so that's a solution not?

Do you think this is the best way to do that?

mlabriolaCorrect answer
Participating Frequently
November 12, 2009

will this work, yes.

is it the best way, probably. that or you could /ake extensions of your classes with public methods that call the protected ones and test those. however, there isnt much more we can give you on this. testing private member functions is not the goal in my mind so i dont have a lot of additional thoughts on doing it.

mike

Participating Frequently
November 9, 2009

Short answer is you don't.

You are testing an object and the object itself is still supposed to be encapsulated, so you need to test this functionality via calls to its public API. When I find myself trying to test functionality that is private I usually take a step back and look at the architecture to see if I am missing anything.

Just my 2 cents,

Mike

Inspiring
November 9, 2009

Private is Private. If you don't want to make it public you can have

another function that does, but that doesnt really change the problem

much.

HTH.

Simeon Bateman

Principle Instigator

PNW Rain LLC

simeon@pnwrain.com

503-616-4405