Rails 3 test case error.on (: field) Vs. Errors [: field]

I am working on test cases of Rails 3. When writing a case, I received a delay error, for example

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)

For this, I used errors [: field] instead of errors.on (: field) Now the obsolescence error has disappeared, but things do not work, as it worked before. It does not test validation for the model.

Sol

+3
source share
4 answers

Replace:

errors.on(:field)

with:

errors.include?(:field)
+9
source

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

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

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

+7
source

- :

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

, -, .

+4
source

I am converting my old specifications into something similar to remove failure warnings:

  model.should have(1).error_on(:field)
  model.should have(:no).errors_on(:field)
+2
source

Source: https://habr.com/ru/post/1771046/


All Articles