# Testing private methods

**URL:** <https://www.ruby-forum.com/t/testing-private-methods/60533>\
**Category:** Ruby\
**Created:** [May 15, 2006, 11:14am UTC](https://www.ruby-forum.com/t/testing-private-methods/60533 "2006-05-15T11:14:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [May 15, 2006, 11:14am UTC](https://www.ruby-forum.com/t/testing-private-methods/60533/1 "2006-05-15T11:14:14Z")

</div>

I have a class with a lot of private utility methods that makes the code  
a lot cleaner. However I want to test them with unit tests. How can I do  
this if they are private?

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [May 15, 2006, 11:32am UTC](https://www.ruby-forum.com/t/testing-private-methods/60533/2 "2006-05-15T11:32:33Z")

</div>

On 5/15/06, Peter H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a class with a lot of private utility methods that makes the code  
> a lot cleaner. However I want to test them with unit tests. How can I do  
> this if they are private?

In your test code do:

class ClassUnderTest  
public :private\_method1, :private\_method2 # …  
end

as exemplified by:

feldt:~$ irb  
irb(main):001:0\> class T  
irb(main):002:1\> private  
irb(main):003:1\> def a; 1; end  
irb(main):004:1\> end  
=\> nil  
irb(main):005:0\> t = T.new  
=\> #\<T:0xb7db90a8\>  
irb(main):006:0\> t.a  
NoMethodError: private method `a’ called for #\<T:0xb7db90a8\>  
from (irb):6  
from :0  
irb(main):007:0\> class T  
irb(main):008:1\> public :a  
irb(main):009:1\> end  
=\> T  
irb(main):010:0\> t.a  
=\> 1  
irb(main):011:0\>

/Robert F.

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [May 15, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/testing-private-methods/60533/3 "2006-05-15T12:13:16Z")

</div>

Thank you that looks to be just what I need.

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [May 15, 2006, 11:32am UTC](https://www.ruby-forum.com/t/testing-private-methods/60533/4 "2006-05-15T11:32:50Z")

</div>

Peter H. wrote:

> I have a class with a lot of private utility methods that makes the code  
> a lot cleaner. However I want to test them with unit tests. How can I do  
> this if they are private?

I believe the standard workaround is to use obj#send(:your\_method).

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![tekhne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/90ced4/32.png) [@tekhne](https://www.ruby-forum.com/u/tekhne)\
**Post date:** [May 15, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/testing-private-methods/60533/5 "2006-05-15T12:55:50Z")

</div>

On 5/15/06, Peter H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you that looks to be just what I need.

Since this is a bit “unsafe” (considering that some dev might use your  
tests as a kind of API/code doc and might miss that they are private  
or whatever) I tend to test my private methods in a separate TestCase  
and/or name the test methods accordingly to make this “unsafeness”  
clearer to the reader.

Example:

require ‘test/unit’

class ClassUnderTest  
public :priv1, :priv2  
end

class UnitTest\_PrivateMethods\_ClassUnderTest \< Test::Unit::TestCase  
def test\_01\_private\_priv1  
end

# …

end

class UnitTest\_ClassUnderTest \< Test::Unit::TestCase

# “normal” tests goes here

end

Not sure if it really works but anyway…

I find the obj.send(:priv\_method) technique a bit too cumbersome to  
write if there are many tests.

Regards,

Robert
