2014-02-12 65 views
0

我得到了一個名爲Candidate的Devise模型。設計模型應該驗證:電子郵件不能爲空(我不測試電子郵件)

而且我得到了這個測試:

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)"] 

如何解決呢?

回答

1

presence: true是一個ActiveRecord驗證,在我看來,它應該被如下叫:(注:驗證並不會驗證)

validates :democracyengine_recipient_id, presence: true 

validate方法將驗證方法或塊的類。當重寫validate實例方法變得太笨重時,這很有用,而且您正在尋找更多描述性聲明來驗證您的驗證。 見API Documentation for validate

的更多詳細信息validates看到官方API Documentation for validates