升级后,uniqueness_of会失败

时间:2016-01-26 08:51:34

标签: ruby-on-rails shoulda

我刚刚升级了我的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)>'

有没有人有过这个问题的经验?

感谢您的帮助,

安东尼

1 个答案:

答案 0 :(得分:2)

有一个已知问题 - https://github.com/thoughtbot/shoulda-matchers/issues/880 - 显然是在主人身上修复过的。

在等待修复时,您现在可以锁定到3.0.1版本,这应该可以正常工作。

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

is_expected.to validate_uniqueness_of(:whatever_field).case_insensitive