Private methode


#1

Hi,

Can I test private methods directly?
Or should I pass by the methods that are calling these private methods?
Or can I make the method private after testing?
What is the best way?

Thank you


#2

IMHO, one should never directly test a private method. So I prefer the
second way you mentioned in your email.

Better yet, think of why you want to test a private method. Is it too
complicated? If this is the case, way too often this private method
should
not really belong to the class it’s currently in. This class might be
doing
too much and this private method is not part of core functionality this
class provides (or else it should be made public as part of the class’s
interface). So you may want to refactor the code to find a proper home
for
your private method and make it public in that class and you can unit
testing this new public method.

Yi


#3

Anita A. removed_email_address@domain.invalid writes:

Hi,

Can I test private methods directly?
Or should I pass by the methods that are calling these private methods?
Or can I make the method private after testing?
What is the best way?

Thank you

The party line is not to test private methods. Either test them
indirectly via a public method, or extract it to a separate object and
make it public there.

Pat


#4

Well, it’s a methods for buying with credit card info and everything. So
I wanted to test it to make sure that all my code works but I can’t make
it public.


#5

On 10 Oct 2008, at 21:18, Anita A. wrote:

but I can’t make it public.

Why not? Who are you afraid is going to call it that shouldn’t?