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

ยทยทยท

--
pdtct

Perhaps:

C:\>qri Module.public

ยทยทยท

On Mar 27, 2:21 pm, "PerfectDayToChaseTornados" <n...@emailaddress.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 :slight_smile: Please could
someone kindly point me in the right direction?

----------------------------------------------------------
Module#public
     public => self
     public(symbol, ...) => self
------------------------------------------------------------------------
     With no arguments, sets the default visibility for subsequently
     defined methods to public. With arguments, sets the named methods
     to have public visibility.

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 3/27/07, PerfectDayToChaseTornados <noone@emailaddress.invalid> 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?

Many Thanks

--
pdtct

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 }

ยทยทยท

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?

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

ยทยทยท

On 3/27/07, Phrogz <gavin@refinery.com> wrote:

Perhaps:

C:\>qri Module.public
----------------------------------------------------------
Module#public
     public => self
     public(symbol, ...) => self
------------------------------------------------------------------------
     With no arguments, sets the default visibility for subsequently
     defined methods to public. With arguments, sets the named methods
     to have public visibility.

"Kyle Schmitt" <kyleaschmitt@gmail.com> wrote in message
news:2b548b8b0703271337n7d1a5badsa8c4f221d4c9f3b2@mail.gmail.com...

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 :slight_smile: I like my unit
testing to be quite fine grained & so do like to test complex private
methods :slight_smile:

ยทยทยท

--
pdtct

"Kyle Schmitt" <kyleaschmitt@gmail.com> wrote in message
news:2b548b8b0703271338x69e330b5r106e54cbeadbab2d@mail.gmail.com...

Perhaps:

C:\>qri Module.public
----------------------------------------------------------
Module#public
     public => self
     public(symbol, ...) => self
------------------------------------------------------------------------
     With no arguments, sets the default visibility for subsequently
     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

ยทยทยท

On 3/27/07, Phrogz <gavin@refinery.com> wrote:

--
pdtct