Permalink
Browse files

The validate_uniqueness_of matcher no longer fails when the subject i…

…s an existing record
  • Loading branch information...
1 parent 02a9753 commit 44ceeb7b1ba9cc16203bde80b4191e92420eb5ed @jferris jferris committed Jan 31, 2009
@@ -65,7 +65,7 @@ def description
end
def matches?(subject)
- @subject = subject
+ @subject = subject.class.new
@expected_message ||= :taken
find_existing &&
set_scoped_attributes &&
@@ -12,13 +12,17 @@ class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
context "with an existing value" do
setup do
- Example.create!(:attr => 'value', :other => 1)
+ @existing = Example.create!(:attr => 'value', :other => 1)
end
should "require a unique value for that attribute" do
assert_accepts validate_uniqueness_of(:attr), @model
end
+ should "pass when the subject is an existing record" do
+ assert_accepts validate_uniqueness_of(:attr), @existing
+ end
+
should "fail when a scope is specified" do
assert_rejects validate_uniqueness_of(:attr).scoped_to(:other), @model
end
@@ -63,14 +67,19 @@ class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
:scope2 => :integer) do
validates_uniqueness_of :attr, :scope => [:scope1, :scope2]
end.new
- Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2)
+ @existing = Example.create!(:attr => 'value', :scope1 => 1, :scope2 => 2)
end
should "pass when the correct scope is specified" do
assert_accepts validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2),
@model
end
+ should "pass when the subject is an existing record" do
+ assert_accepts validate_uniqueness_of(:attr).scoped_to(:scope1, :scope2),
+ @existing
+ end
+
should "fail when a different scope is specified" do
assert_rejects validate_uniqueness_of(:attr).scoped_to(:scope1), @model
end

0 comments on commit 44ceeb7

Please sign in to comment.