# Temporarily change method access for test

**URL:** <https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333>\
**Category:** Ruby\
**Created:** [March 27, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333 "2007-03-27T22:26:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [March 27, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333/1 "2007-03-27T22:26:02Z")

</div>

Hi All,

I have seen an example of a unit test temporarily making a private  
method  
public to unit test it. Now that I need it I can’t find it 🙂 Please  
could  
someone kindly point me in the right direction?

Many Thanks

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [March 27, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333/2 "2007-03-27T22:36:03Z")

</div>

On Mar 27, 2:21 pm, “PerfectDayToChaseTornados”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have seen an example of a unit test temporarily making a private method  
> public to unit test it. Now that I need it I can’t find it 🙂 Please could  
> someone kindly point me in the right direction?

Perhaps:

## C:\>qri Module.public

Module#public  
public =\> self  
public(symbol, …) =\> self

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [March 27, 2007, 10:39pm UTC](https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333/3 "2007-03-27T22:39:04Z")

</div>

Private methods?  
Hum. Never saw a use for them in my stuff… anyway

[http://www.rubycentral.com/faq/rubyfaq-7.html](http://www.rubycentral.com/faq/rubyfaq-7.html) and  
[http://www.rubycentral.com/faq/rubyfaq-8.html](http://www.rubycentral.com/faq/rubyfaq-8.html)

will tell you.

But something like this will work

class J  
def ack()  
puts “be nimble”  
end  
def ill()  
puts “went up the hill”  
end  
private :ack  
end

j=J.new

j.ill  
went up the hill

j.ack  
NoMethodError: private method `ack’ called for #\<J:0x2cf19d8\>

class \<\<j  
public :ack  
end

j.ack  
be nimble

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [March 27, 2007, 11:19pm UTC](https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333/4 "2007-03-27T23:19:11Z")

</div>

On Wed, Mar 28, 2007 at 05:25:08AM +0900, PerfectDayToChaseTornados  
wrote:

> Hi All,
> 
> I have seen an example of a unit test temporarily making a private method  
> public to unit test it. Now that I need it I can’t find it 🙂 Please could  
> someone kindly point me in the right direction?

You can always bypass the checks using instance\_eval. That allows you to  
directly access instance variables of the object too.

class Foo  
private  
def hello  
puts “gotcha”  
end  
end  
a = Foo.new  
a.instance\_eval { hello }

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [March 28, 2007, 12:31am UTC](https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333/5 "2007-03-28T00:31:23Z")

</div>

“Kyle S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> class J
> 
> j.ack  
> be nimble

Thanks!! I figured out I can do it using send as well 🙂 I like my unit  
testing to be quite fine grained & so do like to test complex private  
methods 🙂

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [March 27, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333/6 "2007-03-27T22:41:04Z")

</div>

On 3/27/07, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> to have public visibility.
> 
> ```

Ohh that’s so much cooler than what I woulda done.

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [March 28, 2007, 1:06am UTC](https://www.ruby-forum.com/t/temporarily-change-method-access-for-test/90333/7 "2007-03-28T01:06:28Z")

</div>

“Kyle S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> > ```
> > defined methods to public. With arguments, sets the named methods
> > to have public visibility.
> > 
> > ```
> 
> Ohh that’s so much cooler than what I woulda done.

Did find that in the Ruby book, but couldn’t really figure out how to  
use it  
😉 Bit of a Ruby newbie, many years Java, but not many days Ruby yet?  
Could  
you give me an example?

I’m sure I’ve seen an example somewhere of a unit test that temporarily  
made  
the method in the class it was testing public, but only during the scope  
of  
the test block/method (can’t remember which). I thought it was a really  
cool  
way to do it 😉

Thanks
