Temporarily change method access for test

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 :slight_smile: Please
could
someone kindly point me in the right direction?

Many Thanks

On Mar 27, 2:21 pm, “PerfectDayToChaseTornados”
[email protected] 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 :slight_smile: Please could
someone kindly point me in the right direction?

Perhaps:

C:>qri Module.public

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

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

http://www.rubycentral.com/faq/rubyfaq-7.html and
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

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 :slight_smile: 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 }

“Kyle S.” [email protected] wrote in message
news:[email protected]

class J

j.ack
be nimble

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

On 3/27/07, Phrogz [email protected] wrote:

 to have public visibility.

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

“Kyle S.” [email protected] wrote in message
news:[email protected]

 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
:wink: 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 :wink:

Thanks