开发者

rails 3 test case error.on(:field) Vs. errors[:field]

开发者 https://www.devze.com 2023-01-21 09:39 出处:网络
I am working on the Rails 3 Test cases . While writing case i got Deprecation error like DEPRECATION WARNING: Errors#on have been deprecated, use Errors#[] instead.

I am working on the Rails 3 Test cases . While writing case i got Deprecation error like

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开发者_C百科. (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)

For that i used errors[:field] instead of errors.on(:field) Now Deprecation error is gone , but cases are not working as earlier it was working . It is not testing any validation for the model

Sol


Replace:

errors.on(:field)

with:

errors.include?(:field)


After searching for examples of how to do this without finding anything I ended up doing:

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

Don't know if this is the preferred way but it seems to do the job.


I am using something like this at the moment:

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

Hope it helps someone, right now it is the cleanest solution for me.


I'm converting my old specs to something like this to remove the deprecation warnings:

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

精彩评论

暂无评论...
验证码 换一张
取 消