On Wed, Oct 14, 2009 at 15:36, Joaquin Rivera Padron <joahk...@gmail.com> wrote:
> 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?

Hello, Joaquin.

I typically make these methods public, then test them. I use the
guideline "if it's important enough to test, then it's useful enough
to use somewhere else". I usually end up using the method -- or
something like it -- somewhere else in my code base, then make it
public at that point, anyway.

I have also trusted, for a long time, the idea that if I want to test
it, but it's private, then I really have a small object trying to grow
out of a larger object. I find this especially true when I have three
private methods, all related to each other, in the same class. In that
case I see the clear signal that a smaller object is getting out, so I
let that happen.

Of course, not everyone feels comfortable with so many small objects,
and not everyone feels comfortable making those methods public. If you
don't feel comfortable to do that, then you should look for tricks in
Ruby to let you invoke that private method another way. I think
someone else suggested using __send__() for that. I strongly prefer
not to do that, and when I do, I generally only do it as a first step
towards refactoring the code I'm testing.

Only you can decide what to do, but if you can't decide, then I highly
recommend making the method public, then testing it. If that makes you
dislike the design, then create a new class for the method and move it
there, making it public.

I hope this helps you.
-- 
J. B. (Joe) Rainsberger :: http://www.jbrains.ca ::
http://blog.thecodewhisperer.com
Diaspar Software Services :: http://www.diasparsoftware.com
Author, JUnit Recipes
2005 Gordon Pask Award for contribution to Agile practice :: Agile
2010: Learn. Practice. Explore.
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to