【问题标题】:shoulda-matchers validate_uniqueness_of returns scoped when not scopedshoulda-matchers validate_uniqueness_of 在没有作用域时返回作用域
【发布时间】:2019-02-28 11:05:10
【问题描述】:

我最近升级到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.

我正在使用 devise 以防万一。

有点迷路了,特别是因为它曾经运行良好

非常感谢

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 是的,您可以从规范中删除验证和.ignoring_case_sensitivity
    • 我确实删除了validates :email, uniqueness: { case_sensitive: false },但仍然遇到同样的错误
    • 我使用devise_token_auth 而不是设计,甚至我也面临同样的问题,删除` validates :email, uniqueness: { case_sensitive: false }` 仍然会引发错误Expected the validation not to be scoped to anything, but it was scoped to :provider instead.是因为 devise_token_auth 提供的provider 列吗? @MikeW 你找到解决方案了吗?
    猜你喜欢
    • 2018-10-07
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2012-07-19
    • 2010-09-21
    • 2016-07-13
    相关资源
    最近更新 更多