Created
July 7, 2011 10:02
-
-
Save boyvanamstel/1069221 to your computer and use it in GitHub Desktop.
Test::Unit test for unique property in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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