Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

The validate_uniqueness_of matcher no longer fails when the subject i…

…s an existing record
  • Loading branch information...
commit 44ceeb7b1ba9cc16203bde80b4191e92420eb5ed 1 parent 02a9753
@jferris jferris authored
View
2  lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb
@@ -65,7 +65,7 @@ def description
end
def matches?(subject)
- @subject = subject
+ @subject = subject.class.new
@expected_message ||= :taken
find_existing &&
set_scoped_attributes &&
View
13 test/matchers/validate_uniqueness_of_matcher_test.rb
@@ -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,7 +67,7 @@ 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
@@ -71,6 +75,11 @@ class ValidateUniquenessOfMatcherTest < Test::Unit::TestCase # :nodoc:
@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
Please sign in to comment.
Something went wrong with that request. Please try again.