temporarily change method access for test

Discussion in 'Ruby' started by PerfectDayToChaseTornados, Mar 27, 2007.

  1. 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
    PerfectDayToChaseTornados, Mar 27, 2007
    #1
    1. Advertising

  2. PerfectDayToChaseTornados

    Phrogz Guest

    On Mar 27, 2:21 pm, "PerfectDayToChaseTornados"
    <> 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.
    Phrogz, Mar 27, 2007
    #2
    1. Advertising

  3. PerfectDayToChaseTornados

    Kyle Schmitt Guest

    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 <> 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, Mar 27, 2007
    #3
  4. PerfectDayToChaseTornados

    Kyle Schmitt Guest

    On 3/27/07, Phrogz <> 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.
    Kyle Schmitt, Mar 27, 2007
    #4
  5. 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 }
    Brian Candler, Mar 27, 2007
    #5
  6. "Kyle Schmitt" <> wrote in message
    news:...
    > 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 :)

    --
    pdtct
    PerfectDayToChaseTornados, Mar 27, 2007
    #6
  7. "Kyle Schmitt" <> wrote in message
    news:...
    > On 3/27/07, Phrogz <> 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
    PerfectDayToChaseTornados, Mar 28, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy S.
    Replies:
    6
    Views:
    500
    Jeremy S.
    Jan 22, 2005
  2. Evan Driscoll
    Replies:
    10
    Views:
    456
    Evan Driscoll
    Aug 26, 2009
  3. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    690
    Skybuck Flying
    Oct 4, 2009
  4. Daniel Berger

    Temporarily dropping root access

    Daniel Berger, Feb 1, 2008, in forum: Ruby
    Replies:
    8
    Views:
    145
    Ken Bloom
    Feb 3, 2008
  5. Peter Otten
    Replies:
    0
    Views:
    53
    Peter Otten
    Mar 21, 2014
Loading...

Share This Page