Unit Testing Private Methods

I used to believe that Unit Testing would consider things in a black-box
approach. Can somebody comment on that?

You can however invoke private methods by using send
someobject.send(:somemethod, *args)

as for the instance variables, you could get them doing
iv = someobject.instance_eval { @instance_variable }
assert_whatever …

···

On Thu, Feb 13, 2003 at 07:46:11AM +0900, Travis Whitton wrote:

It seems that in order to be able to unit test a method it has to be
publicly accessible. Is there a way to use test/unit to test private methods
and instance variables?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

*** PUBLIC flooding detected from erikyyy
THAT’s an erik, pholx… :wink:
– Seen on #LinuxGER

Thinking on it again, I should have written
“white-box approach testing only public methods”

Correct?

···

On Thu, Feb 13, 2003 at 08:16:22AM +0900, Mauricio Fernández wrote:

On Thu, Feb 13, 2003 at 07:46:11AM +0900, Travis Whitton wrote:

It seems that in order to be able to unit test a method it has to be
publicly accessible. Is there a way to use test/unit to test private methods
and instance variables?

I used to believe that Unit Testing would consider things in a black-box
approach. Can somebody comment on that?


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

But what can you do with it?
– ubiquitous cry from Linux-user partner

I try to only test public methods, but sometimes run into a situation
where testing private methods is good. This is generally when one
public method calls several large, implementation-heavy private
methods. In this case, the private methods are important, and it’s
easier to test them directly to build some confidence.

In which case

@instance.send(:method)

does the job.

I like the fact that you can call private methods, but the code for
doing so sticks out.

Gavin

···

On Thursday, February 13, 2003, 9:00:29 PM, Mauricio wrote:

It seems that in order to be able to unit test a method it has to be
publicly accessible. Is there a way to use test/unit to test private methods
and instance variables?

I used to believe that Unit Testing would consider things in a black-box
approach. Can somebody comment on that?

Thinking on it again, I should have written
“white-box approach testing only public methods”