2010-10-23 52 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)