# Testing private methods

**URL:** <https://rubytalk.org/t/testing-private-methods/27426>\
**Category:** ruby-talk\
**Created:** [15 May 2006 09:12 UTC](https://rubytalk.org/t/testing-private-methods/27426 "2006-05-15T09:12:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [15 May 2006 09:12 UTC](https://rubytalk.org/t/testing-private-methods/27426/1 "2006-05-15T09:12:33Z")

</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:** ![Robert\_Feldt](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@Robert\_Feldt](https://rubytalk.org/u/Robert_Feldt)\
**Post date:** [15 May 2006 09:29 UTC](https://rubytalk.org/t/testing-private-methods/27426/2 "2006-05-15T09:29:46Z")

</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?

In your test code do:

class ClassUnderTest  
&nbsp;&nbsp;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\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6  
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 Feldt

> **···**
>
> On 5/15/06, Peter Hickman \<peter@semantico.com\> wrote:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [15 May 2006 09:31 UTC](https://rubytalk.org/t/testing-private-methods/27426/3 "2006-05-15T09:31:25Z")

</div>

Peter Hickman 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:** ![Peter\_Hickman](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@Peter\_Hickman](https://rubytalk.org/u/Peter_Hickman)\
**Post date:** [15 May 2006 10:11 UTC](https://rubytalk.org/t/testing-private-methods/27426/4 "2006-05-15T10:11:43Z")

</div>

Thank you that looks to be just what I need.

---

<div class="post-metadata">

**Author:** ![Robert\_Feldt](https://avatars.discourse-cdn.com/v4/letter/r/7993a0/32.png) [@Robert\_Feldt](https://rubytalk.org/u/Robert_Feldt)\
**Post date:** [15 May 2006 10:53 UTC](https://rubytalk.org/t/testing-private-methods/27426/5 "2006-05-15T10:53:19Z")

</div>

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  
&nbsp;&nbsp;public :priv1, :priv2  
end

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

&nbsp;&nbsp;# ...  
end

class UnitTest\_ClassUnderTest \< Test::Unit::TestCase  
&nbsp;&nbsp;# "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

> **···**
>
> On 5/15/06, Peter Hickman \<peter@semantico.com\> wrote:
> 
> > Thank you that looks to be just what I need.
