Ruby on rails Shoulda无法正确验证区分大小写的唯一
使用Ruby on rails Shoulda无法正确验证区分大小写的唯一,ruby-on-rails,shoulda,Ruby On Rails,Shoulda,使用shoulda和FactoryGirl测试模型验证 工厂 FactoryGirl.define do factory :tag do value { Faker::Lorem.word } user end end 标签型号 class Tag < ApplicationRecord validates :value, presence: true, uniqueness: { case_sensitive
shoulda
和FactoryGirl
测试模型验证
工厂
FactoryGirl.define do
factory :tag do
value { Faker::Lorem.word }
user
end
end
标签型号
class Tag < ApplicationRecord
validates :value,
presence: true,
uniqueness: { case_sensitive: false }
belongs_to :user
has_and_belongs_to_many :cards
end
我在运行测试时遇到以下错误
Failures:
1) Tag validations should validate that :value is case-sensitively unique
Failure/Error: it { should validate_uniqueness_of(:value) }
Tag did not properly validate that :value is case-sensitively unique.
After taking the given Tag, setting its :value to ‹"an arbitrary
value"›, and saving it as the existing record, then making a new Tag
and setting its :value to a different value, ‹"AN ARBITRARY VALUE"›,
the matcher expected the new Tag to be valid, but it was invalid
instead, producing these validation errors:
* value: ["has already been taken"]
* user: ["must exist"]
# ./spec/models/tag_spec.rb:6:in `block (3 levels) in <top (required)>'
# -e:1:in `<main>'
故障:
1) 标记验证应验证:值区分大小写唯一
失败/错误:它{应该验证(:值)的唯一性}
标记未正确验证:值是否区分大小写唯一。
获取给定标记后,将其:值设置为è“任意”
值“›”,并将其保存为现有记录,然后制作新标记
并将其:值设置为不同的值,è“任意值”›,
匹配器希望新标记有效,但它无效
相反,产生以下验证错误:
*值:[“已获取”]
*用户:[“必须存在”]
#./spec/models/tag_spec.rb:6:in'block(3层)in'
#-e:1:in`'
测试不区分大小写的正确方法是使用下面的matcher
it { should validate_uniqueness_of(:value).case_insensitive }
您也可以使用忽略案例敏感性编写:
it { should validate_uniqueness_of(:value).ignoring_case_sensitivity }
it { should validate_uniqueness_of(:value).ignoring_case_sensitivity }