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
Failing validate_uniqueness_of(:email) #935
Comments
The matcher is describing the steps it took to run the test. What part of it is confusing? Does your User model have a uniqueness validation on |
Yes my User model has this And I'm using User model with Devise. |
Okay. The reason why this is happening is that Devise overrides The matcher wants to create two records, one with an email address of "josiah_koelpin@wiegand.biz", the other with an email of "JOSIAH_KOELPIN@WIEGAND.BIZ". It expects this to work, because if it works, then it means your uniqueness validation is in place. However, because of Devise, the second user's email gets changed to "josiah_koelpin@wiegand.biz". So the matcher actually ends up trying to save two users with the same email address, and that doesn't work, because the uniqueness validation doesn't allow for that. So the matcher fails. The message that you're getting should be a little more descriptive, and to be honest, I'm not sure why it's not -- I added something extra to detect this in the latest version. In any case, try using: it { should validate_uniqueness_of(:email).ignoring_case_sensitivity } |
Thanks for the information, and it's working now!. I used this.
|
Okay, cool. Glad I could help! I'm going to keep this open to remind myself that we should make the user experience here better. |
Hi, Applicants Model Validations
Test Variations
Exact error
**EditI'm actually thinking this could be better somewhere else or in it's own issue. Sorry. Please advise when you get the chance. |
@batshoes This may not be it, but you have a validation on |
Yikes. |
@batshoes Thank you for posting this. It helps other imperfect programmers, like me. |
So - interestingly enough, I'm having this exact issue. Spec:RSpec.describe Course, type: :model do
describe "validations" do
subject { build(:course) }
it { should validate_presence_of :title }
it { should validate_uniqueness_of :title }
end
end Model:class Course < ApplicationRecord
validates :title, presence: true, uniqueness: true
end
|
Try changing
To
Validations work on creation, like you are doing in the console, but FactoryGirl.build (which is what I am assuming you are using) is making a new unsaved instance
Before running the validations. |
@mminsaus definitely a good thought! It looks like the result is the same. Based on the error message when changing to When the subject is already created - You can see more details in the error message given if the object is created (below) vs when it's just built (in my previous post)
|
I'm having the exact issue described above by @thewatts. I'm running Rails 5, Ruby 2.3.1, PostgreSQL. |
@thewatts I'm not sure why you would be seeing that. However, that seems like a different issue than this issue. Can you copy that to a new one so I can mark it appropriately as a Rails 5 issue? Thank you. |
I'm validating the email is unique, however the test should pass, but the message is incoherent and the error too.
My test:
` subject { build(:user) }
it { should validate_uniqueness_of(:email) }`
`
Failure/Error: it { should validate_uniqueness_of(:email) }
`
The translation is in spanish, in english should be
The text was updated successfully, but these errors were encountered: