Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Forked from bcantin/gist:443610
Created June 23, 2010 23:43
Show Gist options
  • Save tjsingleton/450744 to your computer and use it in GitHub Desktop.
Save tjsingleton/450744 to your computer and use it in GitHub Desktop.
# you can turn two tests into one if you are testing almost the same thing
# example
before(:each) do
@project = Project.new
end
it "should contain errors on a blank url" do
@project.save
@project.errors.should include({:url=>["can't be blank"]})
end
it "should contain erros on a blank name" do
@project.save
@project.errors.should include({:name=>["can't be blank"]})
end
#The above tests are very clear and lets the developer know what is going on, but it is
# duplicated except for the attribute being tested... so lets refactor!
[:url,:name].each do |attrib|
it "should contain errors on a blank #{attrib}" do
@project.save
@project.errors.should include({attrib=>["can't be blank"]})
end
end
end
# You should not feel that you HAVE to refactor the above two into one, as most of the time
# readability in tests should trump the DRY principal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment