[rspec-users] specs on private methods

Shot (Piotr Szotkowski) shot at hot.pl
Fri Jan 11 09:04:09 EST 2008


David Chelimsky:

> On Jan 11, 2008 3:49 AM, Shot (Piotr Szotkowski) <shot at hot.pl> wrote:

>> One run of by_input_sets!() takes a couple of seconds, so can
>> be tested; one run of decompose!() takes much longer, so to test
>> decompose!() I should stub by_input_sets!() so it returns canned
>> data (right?).

> In TDD there is a rule of thumb that says don't stub a method in the
> same class as the method you're testing. The risk is that as the real
> implementation of by_input_sets!() changes over time, it has access to
> internal state that could impact the behaviour of decompose!().

Ah, well pointed.

> It also sounds like by_input_sets!() has enough independent
> responsibility that it should actually be public.

It actually started out as a public method and only this
very thread made me consider switching it to private. :)

> The standard approach here, which is as much a result of OO principles
> as TDD, is to extract a class with the by_input_sets!() exposed as
> a public method. You mock that object in your Decomposition examples
> and then you have public access to by_input_sets!() on the new class.

Ok, makes sense, thanks – this definitely
made me think about the design.

What about the general issue of a slow method that is fast enough to be
testable in isolation, but some other method from the same class calls
it so many times that it needs to be stubbed for that method to be
spec’d in a sane manner?

-- Shot
-- 
Fail hard. Fail with motherfucking gusto. Succeeding, like flying,
is throwing yourself to the ground and missing.     -- Jeff Hodges
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 191 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-users/attachments/20080111/f1b42214/attachment.bin 


More information about the rspec-users mailing list