Forum: Ruby on Rails about private method test

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
669c735cf55efa005140fa232b05c4f6?d=identicon&s=25 daociyiyou (Guest)
on 2008-11-14 16:17
(Received via mailing list)
For the methods defined as private,we need to test it?
For the reason that the private methods are used within the public
methods,so we need not to test the private methods as long as the
public methods are tested?
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2008-11-14 16:57
(Received via mailing list)
it's up to you.  if you're satisfied that the private method is
working properly in the course of testing the public methods then
fine, otherwise you should be able to call the private method like:

@my_object.send(:private_method_name, arg1, arg2)
90e140f8553ecdec1b0d7dbf608a0561?d=identicon&s=25 Pau Cor (phylae)
on 2008-11-14 17:01
daociyiyou wrote:
> For the methods defined as private,we need to test it?
> For the reason that the private methods are used within the public
> methods,so we need not to test the private methods as long as the
> public methods are tested?


A very clear discussion of the topic by one of the best testers in the
Ruby world (in my opinion):
http://evang.eli.st/blog/2008/10/27/testing-protec...

HTH
Paul
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2008-11-14 17:27
Jeff Emminger wrote:
> it's up to you.  if you're satisfied that the private method is
> working properly in the course of testing the public methods then
> fine, otherwise you should be able to call the private method like:
>
> @my_object.send(:private_method_name, arg1, arg2)

I believe that I read somewhere that a future version of Ruby will be
"fixed" to not allow using "send" to call private and protected methods.
Which is really the behavior it should have had in the first place.
Allowing this breaks the rules of encapsulation.

A test case class is external to the class being tested, and like any
other class SHOULD NOT have access to private data. Given that a private
method should only ever be called from inside the confines of the class
then you must access them via the public API and therefore tested though
that public API.

Test cases are clients of classes being tested in the same way any other
class would be a client. This means that your tests, or specs, should
test the behavior of the class through that same API. Theses tests
should therefore be complete enough to exercise the "behavior" of the
underlying private data and methods of the class being tested.

At least this is my own understanding. Feel free to disagree...
D69d23d8e811e8ab2a8593380d6ede63?d=identicon&s=25 Jeff Emminger (jemminger)
on 2008-11-14 23:08
(Received via mailing list)
On Nov 14, 11:27 am, Robert Walker <rails-mailing-l...@andreas-s.net>
wrote:
> Allowing this breaks the rules of encapsulation.
> should therefore be complete enough to exercise the "behavior" of the
> underlying private data and methods of the class being tested.
>
> At least this is my own understanding. Feel free to disagree...
> --

Yeah, that's probably the better route.  If you're resorting to
calling a protected or private method directly, then you either aren't
testing the public methods well enough or you haven't designed the
software well enough
669c735cf55efa005140fa232b05c4f6?d=identicon&s=25 daociyiyou (Guest)
on 2008-11-16 10:54
(Received via mailing list)
Thank you very much! I have understood it very clearly.
669c735cf55efa005140fa232b05c4f6?d=identicon&s=25 daociyiyou (Guest)
on 2008-11-16 10:55
(Received via mailing list)
Thank you very much! I have understood it very clearly.
This topic is locked and can not be replied to.