[rspec-users] specs on private methods

David Chelimsky dchelimsky at gmail.com
Wed Jan 9 08:43:18 EST 2008

On Jan 9, 2008 7:26 AM, Richard Conroy <richard.conroy at gmail.com> wrote:
> On Jan 8, 2008 7:14 PM, Daniel Tenner <daniel.ruby at tenner.org> wrote:
> > Might be a personal thing, but my approach is that I try to test the
> > public behaviour of the object. Testing private methods is, imho,
> > getting dangerously close to specifying how the object does its
> > business, rather than what it does.
> >
> > I would just spec the externally visible behaviour, where it occurs,
> > and let the object implement it as it wants (using private methods or
> > any other mechanism).
> The trouble is that this makes it very difficult to get effective coverage.
> There is usually a lot more setup involved. If your tests are more expensive
> to write, because you are unit testing indirectly through public methods,
> less testing will get done,

If you live by the rule of "never write a line of code except to get a
failing example to pass," then you should always have 100% coverage
implicitly. If you're back-filling that's a different story, but then
we're not talking about BDD anymore.

I'm not saying that I will never test private methods, but it's
something that should be rare and avoided.


> and worse tests can end up getting very brittle.
> I do like the idea though of during the unit test itself, you unseal the method.
> You get the best of both worlds.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list