-
-
Notifications
You must be signed in to change notification settings - Fork 909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug? in uniqueness verification when pure number was generated #1059
Comments
Hey John, Sorry you got this error. So what's happening here? By default, the uniqueness matcher tests not only that there is a uniqueness validation present in your model, but that this validation works case-sensitively. It does this by asserting not only that two records that have the same exact value for an attribute fail validation, but that if you swap the case of one of the values, then both records will pass validation. For instance, say you had a Person model with a uniqueness validation on describe Person do
it { should validate_uniqueness_of(:name) }
end It would basically translate to this: describe Person do
it "fails validation when another Person with the same name exists" do
# Initially there is a conflict...
first_person = Person.create!(name: "John")
second_person = Person.new(name: "John")
second_person.validate
expect(second_person.errors[:name]).to include("has already been taken")
# Now we swap the case
second_person.name = "john"
# The conflict is cleared
expect(second_person.errors[:name]).to be_empty
end
end But this assumes that the value of describe Person do
it "fails validation when another Person with the same name exists" do
# Initially there is a conflict...
first_person = Person.create!(name: "123")
second_person = Person.new(name: "123")
second_person.validate
expect(second_person.errors[:name]).to include("has already been taken")
# Now we swap the case?
second_person.name = "123"
# Oops, this fails!
expect(second_person.errors[:name]).to be_empty
end
end Hence, a while back we chose to make an interception in this case and raise an explicit error instead of allowing the matcher to fail (which would be even more confusing). In your case, it sounds like since you're using a random value for To fix this, I would modify your Office model so that it do
office = build(:office, activation_token: "abc123")
expect(office).to validate_uniqueness_of(:activation_token)
end Hope this helps. |
Was surprised to suddenly see this on CI for a PR that only updated the rollbar gem...
The relevant code:
and the relevant spec
sidenote -- folks from the NYC Thoughtbot office wrote a lot of this code (Mike Wenger, looking at you!)
I really don't understand the details of how the test values are generated even after digging through the source and unit tests for the uniqueness matcher so I'm stymied. Just surprised this would be a surprise and hard-to-replicate failure.
We're setting the value of
activation_token
using SecureRandom.hex(16) so I believe what happened is we got a "pure" number instead of 312af567 or something, thus the surprise failure. We're on PG9.6 so case sensitivity is the default in the db, but for the hex value it shouldn't matter.Thoughts?
The text was updated successfully, but these errors were encountered: