shoulda-matchers validate_uniqueness_of returns scoped when not scoped

shoulda-matchers validate_uniqueness_of returns scoped when not scoped

我最近升级到rails 5,同时升级了gem shoulda-matchers。

我有一个具有 email 属性的 User 模型 电子邮件应该是唯一的,并且这种唯一性不区分大小写

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

我正在使用 rspec

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

但是它抛出这个错误

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.

我正在使用设计以防这可能有所帮助。

有点迷路了,特别是因为它以前工作得很好

非常感谢

如果您使用 validatable module,Devise 会在电子邮件上添加唯一性验证。验证可以被认为是累积的,因此 validates :email, uniqueness: { case_sensitive: false } 不会替换现有的验证 - 它只是添加另一个验证。

您需要做的是删除可验证模块:

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

并自己实施验证。但我真的会首先考虑这是否是个好主意。您真的想要 foo@example.comFoo@example.com 的副本吗?