Skip to content
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

Shoulda Matchers ActiveRecord Validate Uniqueness #1564

Open
fabriciobonjorno opened this issue Jun 17, 2023 · 1 comment
Open

Shoulda Matchers ActiveRecord Validate Uniqueness #1564

fabriciobonjorno opened this issue Jun 17, 2023 · 1 comment

Comments

@fabriciobonjorno
Copy link

fabriciobonjorno commented Jun 17, 2023

Hello everything is fine? I'm having some problems doing unique validations with the gem following my models and tests.

Model User
validates :name, :username, :email, :password_digest, presence: true validates :email, :username, uniqueness: { case_sensitive: false }

Model Phone
validates :phone_type, :phone_number, presence: true validates :phone_number, uniqueness: { case_sensitive: false }

Spec User
it { is_expected.to validate_presence_of(:email) } it { is_expected.to validate_uniqueness_of(:email).case_insensitive } it { is_expected.to validate_presence_of(:username) } it { is_expected.to validate_uniqueness_of(:username).case_insensitive }

Spec Phone
it { is_expected.to validate_presence_of(:phone_number) } it { is_expected.to validate_uniqueness_of(:phone_number).case_insensitive }

My factories

User
factory :user do name { Faker::Name.name } email { Faker::Internet.email } password_digest { Faker::Internet.password(min_length: 8) } token_password_recovery { rand(1..999) } token_password_recovery_deadline { '2023-06-17 15:28:41' } token_password_confirmation { rand(1..999) } token_password_confirmation_deadline { '2023-06-17 15:28:41' } company username { Faker::Internet.username } end

Phone
factory :phone do phone_type { rand(0..1) } phone_number { Faker::PhoneNumber.phone_number_with_country_code } after :build, &:phoneble end

error 3
error 2
error 1

@cmpprg
Copy link

cmpprg commented Jun 28, 2023

For email and username tests. It looks like your tests expects it not to be scoped_to anything. I'm guessing based on the results that you have it scoped to :company in your model validation.

Try adding .scoped_to(:company) in your test cases and see if it helps.

it { is_expected.to validate_uniqueness_of(:email).case_insensitive.scoped_to(:company) }

I don't specifically see it scoped in the examples you give, but perhaps you have some 3rd party like Tenancy scoping this in your models.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants