应允许空白 validate_uniqueness_of

发布于 2024-09-05 09:44:15 字数 106 浏览 9 评论 0原文

使用应该,任何想法如何在进行此测试时允许空白:

should validate_uniqueness_of(:email)

谢谢。

Using shoulda, any ideas how to allow blank when having this test:

should validate_uniqueness_of(:email)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-09-12 09:44:15

我已经想通了!! validate_uniqueness_of 依赖于您当前在数据库中的第一条记录上测试的属性的值。因此,如果数据库中的第一条记录恰好为空白或该属性的 nul,您的测试将始终给出如下错误:“失败/错误:it { should validate_uniqueness_of(:customer_number).case_insensitive.scoped_to(:user_id) }当 customer_number 设置为 nil 时,预期错误包括“已被采取”,没有错误”。

那么我们该如何解决这个问题呢?确保删除所有现有记录,并且第一条记录包含填充的属性值。

型号:

validates_uniqueness_of :customer_number, :scope => :user_id, :case_sensitive => false, :allow_blank => true, :allow_nil => true

型号规格:

describe 'Validation' do

  it { should allow_value('').for(:customer_number) }

  describe 'When a user exists with the same customer_number and user' do
    before(:each) do 
      Customer.destroy_all
      # Saving a single customer to validate uniqueness.
      @existing = Factory(:customer, :customer_number => 'test') # If you leave customer_number blank here the matcher will check if it can save a new record with a blank customer_number which is possible since we allow blanks. So make sure your first record has a filled in value!!
    end

    subject do
      Factory.build(:customer)
    end

    it { should validate_uniqueness_of(:customer_number).case_insensitive.scoped_to(:user_id) }
  end
end

希望这可以解决一些人的问题:)

I have figured it out!! The validate_uniqueness_of relies on the value for the attribute you are currently testing on the first record in the database. So, if the first record in the database happens to be blank or nul for that attribute your test will always give an error like this: "Failure/Error: it { should validate_uniqueness_of(:customer_number).case_insensitive.scoped_to(:user_id) } Expected errors to include "has already been taken" when customer_number is set to nil, got no errors".

So how do we fix this? Make sure you delete all existing records and that the first record contains a filled in value for the attribute.

model:

validates_uniqueness_of :customer_number, :scope => :user_id, :case_sensitive => false, :allow_blank => true, :allow_nil => true

model spec:

describe 'Validation' do

  it { should allow_value('').for(:customer_number) }

  describe 'When a user exists with the same customer_number and user' do
    before(:each) do 
      Customer.destroy_all
      # Saving a single customer to validate uniqueness.
      @existing = Factory(:customer, :customer_number => 'test') # If you leave customer_number blank here the matcher will check if it can save a new record with a blank customer_number which is possible since we allow blanks. So make sure your first record has a filled in value!!
    end

    subject do
      Factory.build(:customer)
    end

    it { should validate_uniqueness_of(:customer_number).case_insensitive.scoped_to(:user_id) }
  end
end

Hope this cleared up some people's issues :)

淡淡的优雅 2024-09-12 09:44:15
should allow_value(" ").for(:email)
should allow_value(nil).for(:email)
should allow_value(" ").for(:email)
should allow_value(nil).for(:email)
满意归宿 2024-09-12 09:44:15

或许

should validate_uniqueness_of(:email, :allow_blank => true)

maybe

should validate_uniqueness_of(:email, :allow_blank => true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文