2010-10-23 21 views
3

我正在使用Rails 3測試用例。在寫情況下,我得到了棄用錯誤,如rails 3測試用例error.on(:field)vs。錯誤[:field]

DEPRECATION WARNING: Errors#on have been deprecated, use Errors#[] instead. 
Also note that the behaviour of Errors#[] has changed. Errors#[] now always returns an Array. An empty Array is returned when there are no errors on the specified attribute. (called from on at /usr/local/lib/ruby/gems/1.9.1/gems/activemodel-3.0.0.rc/lib/active_model/deprecated_error_methods.rb:7) 

對於我使用的錯誤[:現場]代替errors.on(:場) 現在棄用錯誤消失,但情況並不像早期的工作是工作。這不是模型

測試任何驗證索爾

+0

什麼是你的問題? – shingara 2010-10-23 11:09:09

+0

問題是errors.on(:field)在rails單元測試中顯示deprecation錯誤和錯誤[:field]無法正常工作..有沒有什麼辦法可以解決這個問題。 – 2010-10-23 11:26:53

回答

7

尋找如何做到這一點沒有任何發現後的例子我落得這樣做:

errors[:field].present?/errors[:field].blank? 

不知道這是否是首選的方式,但它似乎做的工作。

+1

錯誤[:field] .blank?爲我工作 - 謝謝 – shedd 2011-06-14 23:02:17

2

我將我的舊規範,以這樣的去除廢棄警告:

model.should have(1).error_on(:field) 
    model.should have(:no).errors_on(:field) 
4

我使用像這樣的時刻:

@hamburger.errors[:ingredients].count.should == 1 
@hamburger.errors[:ingredients].should include "Tomatoes are missing dude!" 

希望它可以幫助別人,現在它對我來說是最乾淨的解決方案。

9

替換:

errors.on(:field) 

有:

errors.include?(:field)