[rspec-users] Eliminating duplication from tests

Brandon Olivares programmer2188 at gmail.com
Fri Apr 10 00:14:24 EDT 2009


Lately I've found myself trying to reduce typing as much as possible, so
when I have a lot of tests that only differ in a few variables, I tend to
abstract them to reduce typing.

For instance, I have an assert_form_errors method that I call within a
describe block to ensure the proper errors are displaying. I have an
assert_form_fields method to validate that the fields that have errors are
wrapped in div.error, and the ones that don't are not (that method should
probably be renamed now that I think of it, but I'm not sure to what). That
one is especially useful because it can test all of the fields in one go.

So is this necessarily bad? I've heard it's not always good to reduce
duplication in tests, but it just seems much preferable than copy/paste.

Also, I fear it is becoming more test-like than behavior, but that's the way
my testing is progressing.


More information about the rspec-users mailing list