temporarily change method access for test

  • Thread starter PerfectDayToChaseTornados
  • Start date
P

PerfectDayToChaseTornados

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
 
P

Phrogz

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
 
B

Brian Candler

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 }
 
P

PerfectDayToChaseTornados

Kyle Schmitt said:
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

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 :)
 
P

PerfectDayToChaseTornados

Kyle Schmitt said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top