Newbie: Unit testing a private method with arguments

Discussion in 'Ruby' started by Marcus Lindemann, Oct 26, 2005.

  1. Hi,
    I'm somewhat new to ruby. and I want to unit test my methods. Since I
    compose my public methods from private ones, I start writing and also
    test those first. From the web I found that testing of private methods
    is possible through

    class Foo
    def foobar
    bar
    end

    private
    def bar # <-- takes no params
    "bar"
    end

    def bla(aName)
    aName
    end
    end

    x = Foo.new
    x.send:)bar)

    However, when applying this to method bla, I don't have any idea on how
    to pass any argument to bla. Can someone help me here?

    Thanks
    Marcus
     
    Marcus Lindemann, Oct 26, 2005
    #1
    1. Advertising

  2. Marcus Lindemann

    Eric Hodel Guest

    On Oct 26, 2005, at 12:12 PM, Marcus Lindemann wrote:

    > Hi,
    > I'm somewhat new to ruby. and I want to unit test my methods. Since
    > I compose my public methods from private ones, I start writing and
    > also test those first. From the web I found that testing of private
    > methods is possible through
    >
    > class Foo
    > def foobar
    > bar
    > end
    >
    > private
    > def bar # <-- takes no params
    > "bar"
    > end
    >
    > def bla(aName)
    > aName
    > end
    > end
    >
    > x = Foo.new
    > x.send:)bar)
    >
    > However, when applying this to method bla, I don't have any idea on
    > how to pass any argument to bla. Can someone help me here?


    An easier way to do it is:

    private unless $TESTING

    But, send takes 1 or more arguments:

    x.send :bla, 'Marcus Lindemann'

    (And passes blocks along as well.)

    --
    Eric Hodel - - http://segment7.net
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
     
    Eric Hodel, Oct 26, 2005
    #2
    1. Advertising

  3. $ ri send

    ------------------------------------------------------------ Object#send
    obj.send(symbol [, args...]) => obj
    obj.__send__(symbol [, args...]) => obj
    ------------------------------------------------------------------------
    Invokes the method identified by _symbol_, passing it any
    arguments
    specified. You can use +_send_+ if the name +send+ clashes with an
    existing method in _obj_.

    On 26 Oct 2005, at 8:12pm, Marcus Lindemann wrote:

    > Hi,
    > I'm somewhat new to ruby. and I want to unit test my methods. Since
    > I compose my public methods from private ones, I start writing and
    > also test those first. From the web I found that testing of private
    > methods is possible through
    >
    > class Foo
    > def foobar
    > bar
    > end
    >
    > private
    > def bar # <-- takes no params
    > "bar"
    > end
    >
    > def bla(aName)
    > aName
    > end
    > end
    >
    > x = Foo.new
    > x.send:)bar)
    >
    > However, when applying this to method bla, I don't have any idea on
    > how to pass any argument to bla. Can someone help me here?
    >
    > Thanks
    > Marcus
    >
    >
     
    Derek Chesterfield, Oct 27, 2005
    #3
    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. jimgardener

    unit testing private methods

    jimgardener, Aug 13, 2008, in forum: Java
    Replies:
    4
    Views:
    1,140
  2. John Maclean
    Replies:
    1
    Views:
    364
    Martin P. Hellwig
    Apr 13, 2010
  3. Ulrich Eckhardt

    unit-profiling, similar to unit-testing

    Ulrich Eckhardt, Nov 16, 2011, in forum: Python
    Replies:
    6
    Views:
    358
    Roy Smith
    Nov 18, 2011
  4. Bill Mosteller
    Replies:
    0
    Views:
    249
    Bill Mosteller
    Oct 22, 2009
  5. Avi
    Replies:
    0
    Views:
    512
Loading...

Share This Page