[rspec-users] Eliminating duplication from tests

Brandon Olivares programmer2188 at gmail.com
Fri Apr 10 10:25:21 EDT 2009



> -----Original Message-----
> From: rspec-users-bounces at rubyforge.org [mailto:rspec-users-
> bounces at rubyforge.org] On Behalf Of Phlip
> Sent: Friday, April 10, 2009 8:39 AM
> To: rspec-users at rubyforge.org
> Subject: Re: [rspec-users] Eliminating duplication from tests
> 
> Refactor production code (while passing the tests after each edit) to
> make it as
> DRY as possible. This _typically_ means duplicate the behavior you need
> to
> change, make the new method pass the new test, and only then merge the
> new
> behavior back in with the old code. Merge following the rule "refactor
> low
> hanging fruit". Don't jam two big methods back together; use Extract
> Method
> Refactor on the smallest common lines within them, until only the
> differences
> are left.

I already factor when I see duplication in the code, but I haven't ever seen
what you are saying about duplicating the behavior in a new method. Usually
I just change the existing method. Can you explain this a bit further?

> 
> This implies clone-and-modify is part of the process. In test code, the
> best way
> to write a new test is clone the best example and change the assertions
> in the
> clone. (Ripping all the old assertions out, and changing the sample
> data on
> principle, are both best practices here.)
> 
> Refactor test code with two further constraints: Unlike production
> code, each
> test case should tell a little story. And squeezing that last bit of
> duplication
> out of the test cases is not likely to improve the tests' designs.
> 
> New tests should always be easy to write. If you get that, then the
> refactoring
> is working.

Thank you. That helps a lot.

Brandon



More information about the rspec-users mailing list