Simple vs. DRY Rails Specs

Posted by John Wulff Sun, 08 Apr 2007 01:16:00 GMT

I’m toying with two different styles of Rails Specification (RSpec) testing. One is easier to read, the other is much more DRY (do not repeat yourself). Which do you think is better?

DRY:
specify "has many ContentTerms" do
  content_term_ids = [ 1, 2, 3 ]
  @content.content_terms.should have(content_term_ids.size).records
  for id in content_term_ids
    @content.content_terms.should include(ContentTerm.find(id))
  end
end
Simple:
specify "has many ContentTerms" do
  @content.content_terms.should have(3).records
  @content.content_terms.should include(ContentTerm.find(1))
  @content.content_terms.should include(ContentTerm.find(2))
  @content.content_terms.should include(ContentTerm.find(3))
end

The DRY version makes me feel special but the simpler version is much easier on my brain. I think I’ll go with the simple one… for now.

Trackbacks

Use the following link to trackback from your own site:
http://www.johnwulff.com/trackbacks?article_id=1

Leave a comment

Comments