Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Shoulda无法正确验证区分大小写的唯一_Ruby On Rails_Shoulda - Fatal编程技术网

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 }