Skip to content

Instantly share code, notes, and snippets.

@bcantin
Created June 18, 2010 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bcantin/443610 to your computer and use it in GitHub Desktop.
Save bcantin/443610 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!
it "should contain errors on a blank url or name" do
@project.save
[:url,:name].each do |attrib|
@project.errors.should include({attrib=>["can't be blank"]})
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