Newbie: Unit testing a private method with arguments

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

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.)

···

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?

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

x.send(:bla, "my-argument")

···

--- Ursprüngliche Nachricht ---
Von: Marcus Lindemann <mli@removethis.shebang.de>
An: ruby-talk@ruby-lang.org (ruby-talk ML)
Betreff: Newbie: Unit testing a private method with arguments
Datum: Thu, 27 Oct 2005 04:12:03 +0900

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

$ 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

Eric Hodel <drbrain@segment7.net> writes:

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

Wonderful trick, thanks a lot!

···

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

Eric Hodel - drbrain@segment7.net - http://segment7.net

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org