Mike W
Mike W

Reputation: 401

shoulda-matchers validate_uniqueness_of returns scoped when not scoped

I recently upgraded to rails 5, and at the same time upgraded the gem shoulda-matchers.

I have a User model with an email attribute the email should be unique, and this uniqueness is case insensitive

class User < ApplicationRecord
   validates :email, uniqueness: { case_sensitive: false }
end

I'm testing this using rspec

RSpec.describe User, type: :model do
  subject { build(:user) }
  [...]
     it { is_expected.to validate_uniqueness_of(:email).ignoring_case_sensitivity }
end

But it throws this error

Expected User to validate that :email is unique, but this could not be proved.
Expected the validation not to be scoped to anything, but it was scoped to :provider instead.

I'm using devise in case this could help.

kinda lost here, specially since it used to be working fine

thanks a lot

Upvotes: 3

Views: 961

Answers (1)

max
max

Reputation: 102443

Devise adds a uniqueness validation on email if you are using the validatable module. Validations can be thought of as cumulative so validates :email, uniqueness: { case_sensitive: false } does not replace the existing validation - it just adds a another validation.

What you need to do is remove the validateable module:

devise :invitable, :omniauthable, :database_authenticatable, :registerable,
         :confirmable, :recoverable, :rememberable, :trackable

And implement the validations yourself. But I would really consider if it a good idea in the first place. Do you really want duplicates of [email protected] and [email protected]?

Upvotes: 3

Related Questions