[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

temporarily change method access for test

PerfectDayToChaseTornados

3/27/2007 8:22:00 PM

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

--
pdtct


6 Answers

Gavin Kistner

3/27/2007 8:35:00 PM

0

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

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

3/27/2007 8:38:00 PM

0

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

http://www.rubycentral.com/faq/ruby... and
http://www.rubycentral.com/faq/ruby...

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

Kyle Schmitt

3/27/2007 8:39:00 PM

0

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.
Ohh that's so much cooler than what I woulda done.

Brian Candler

3/27/2007 9:18:00 PM

0

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 }

PerfectDayToChaseTornados

3/27/2007 10:26:00 PM

0


"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/ruby... and
> http://www.rubycentral.com/faq/ruby...
>
> 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 :-)

--
pdtct


PerfectDayToChaseTornados

3/27/2007 11:05:00 PM

0

"Kyle Schmitt" <kyleaschmitt@gmail.com> wrote in message
news:2b548b8b0703271338x69e330b5r106e54cbeadbab2d@mail.gmail.com...
> 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.
> 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
--
pdtct