2014-10-19 79 views
0

我正在爲我的用戶模型編寫簡單的模型測試。其中一人會檢查:email屬性的存在:電子郵件驗證錯誤在obj.errors.full_messages(Rails 3.2,Devise,RSpec)中出現兩次

it "is invalid without an email" do 
    expect(user = User.new(email: nil)).to have(1).errors_on(:email) 
    end 

出於某種原因,本次測試失敗,因爲錯誤的散列包括「電子郵件不能爲空」兩次:

Failures: 

    1) User is invalid without an email 
    Failure/Error: expect(user = User.new(email: nil)).to have(1).errors_on(:email) 
     expected 1 errors on :email, got 2 

當我在控制檯創建nil向用戶發送電子郵件和呼叫user.errors.full_messages我得到如下:

@messages= 
    {:email=>["can't be blank", "can't be blank"] 

這是我的驗證看起來像我ñuser.rb

validates_presence_of :email 

奇怪的是,如果我從user.rb除去上面的代碼測試通過了,我只得到了錯誤的散列錯誤一次。

這是怎麼回事?我應該注意到我的用戶模型是通過Devise生成器創建的。 Devise是否已經包含對:email的驗證?

回答

3

是的。你的假設是正確的。設計有它自己的驗證電子郵件不是空白的。你可以刪除你的。