簡體   English   中英

Devise Model Shoulda驗證:電子郵件不能為空(我沒有測試電子郵件)

[英]Devise Model Shoulda Validation: Email can't be blank (I'm not testing Email)

我得到了一個名為候選人的設計模型。

我得到了這個測試:

it { expect(subject).to validate_presence_of(:democracyengine_recipient_id) } 

模型中的這一行:

validates :democracyengine_recipient_id, presence: true

並且驗證有效(在其他測試中)。

但是在這個特定的測試中,我得到了這個錯誤:

Failure/Error: it { expect(subject).to validate_presence_of(:democracyengine_recipient_id) }
Expected errors to include "can't be blank" when democracyengine_recipient_id is set to nil, got errors: ["email can't be blank (\"\")", "password can't be blank (nil)"]

我如何解決它?

presence: true是一個ActiveRecord驗證器,我認為應該按以下方式調用它:(注意:驗證而不是驗證)

validates :democracyengine_recipient_id, presence: true

validate方法向類添加驗證方法或塊。 當重寫validate實例方法變得太笨拙並且您正在尋找驗證的更具描述性的聲明時,這很有用。 請參閱API文檔中的更多詳細信息以進行驗證

有關validates請參見官方API文檔進行驗證

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM