【问题标题】:uniqueness_of fails after upgrade shoulda matchers升级应该匹配器后uniqueness_of失败
【发布时间】:2016-01-26 08:51:33
【问题描述】:

我刚刚升级了我的 Rails 项目以使用 Shoulda Matchers 3.0

但是,验证 title 属性唯一性的模型测试现在失败了:

app/models/product.rb

it { is_expected.to validate_uniqueness_of(:title) }

spec/models/product_spec.rb

it { is_expected.to validate_uniqueness_of(:title) }

测试失败的错误信息是:

Product should validate that :title is case-sensitively unique
 Failure/Error: it { is_expected.to validate_uniqueness_of(:title) }
   Product did not properly validate that :title is case-sensitively
   unique.
     The record you provided could not be created, as it failed with the
     following validation errors:

     * title: ["can't be blank"]
     * description: ["can't be blank"]
 # ./spec/models/product_spec.rb:6:in `block (2 levels) in <top (required)>'

有人遇到过这个问题吗?

感谢您的帮助,

安东尼

【问题讨论】:

    标签: ruby-on-rails shoulda


    【解决方案1】:

    有一个已知问题 - https://github.com/thoughtbot/shoulda-matchers/issues/880 - 显然已在 master 中修复。

    在等待修复期间,您可以暂时锁定到 3.0.1 版本,这应该可以工作。

    唯一性匹配器在 3.0.x 中确实更改为默认区分大小写以匹配默认的 rails 验证器,因此如果您将 rails 验证设置为不区分大小写,则必须将匹配器调用更新为

    is_expected.to validate_uniqueness_of(:whatever_field).case_insensitive
    

    【讨论】:

    • 谢谢汤姆。我想我会等待新版本的应该匹配器
    • 更新了如果您在 3.0.1 中也遇到错误需要做什么的详细信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多