2009/10/14 Joaquin Rivera Padron <joahk...@gmail.com>

> hello there,
> how do you tipically spec private methods? The thing is Ï have something
> like this:
>
> def some_method
>    complex_method + other_complex_methods
> end
>
> private
> def complex_method...
> def other_complex_methods ...
>
> and the two complex methods can get really tricky to get right, I would
> like to be able to write specs for them, how do you do that? I mean I cannot
> do:
>
> object.some_private_method
>
> am I?
>
> thanks in advance,
> joaquin
>
> --
> www.least-significant-bit.com
>
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users


An interesting standard to apply to your complex methods (well I think its
interesting and valuable) is this

    A method can either do one thing or call other methods but not both.
    A method cannot be more than 5 lines long (insert a bigger number if you
must ...)
    If a method consists of calls to other methods then the names of these
methods should document the method.

If you apply this to refactor your existing code you should end up with a
hierarchy of small private methods with leaves that do tiny little tasks and
nodes that describe how tasks are done. I find this beneficial in itself
(the code is much easier to read) as well as being a good first step to
extracting new objects. Nodes in the old object become public methods and
their leaves come along as private methods in the new object.

You can take this further and state that no public method should ever do
anything, it should just contain calls to private methods which document how
the public method works. I expect most would consider that going to far ...
sigh

Andrew
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to