Skip to content

Instantly share code, notes, and snippets.

@boyvanamstel
Created July 7, 2011 10:02
Show Gist options
  • Save boyvanamstel/1069221 to your computer and use it in GitHub Desktop.
Save boyvanamstel/1069221 to your computer and use it in GitHub Desktop.
Test::Unit test for unique property in Rails
# Test in test/functional/sites_controller_test.rb
test "should be unique" do
link1 = sites(:one)
link1.save
link2 = Site.new(sites(:two))
link2.url = link1.url
assert !link2.save, "Saved a link with the same url"
end
# Implementation in app/model/site.rb
class Site < ActiveRecord::Base
validates_uniqueness_of :url
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment