diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2018-11-21 00:49:01 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-21 00:49:01 -0500 |
commit | 32695069b10a79c13312b189d6508f8ed4c6186a (patch) | |
tree | 4d52961dadfbfdb1a137da25880ead554edfc69f /activemodel/test | |
parent | 9fb53e7b198a0b497566f06ba938d66502fa4326 (diff) | |
parent | ee2b84f3cbd97d8116467ea0d44b8182824c83ab (diff) | |
download | rails-32695069b10a79c13312b189d6508f8ed4c6186a.tar.gz rails-32695069b10a79c13312b189d6508f8ed4c6186a.tar.bz2 rails-32695069b10a79c13312b189d6508f8ed4c6186a.zip |
Merge pull request #34489 from 6temes/add_slice_method_to_errors
Add slice! method to ActiveModel::Errors
Diffstat (limited to 'activemodel/test')
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 185b5a24ae..f9015b869d 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -411,6 +411,30 @@ class ErrorsTest < ActiveModel::TestCase assert_equal({ name: [{ error: :blank }, { error: :invalid }] }, person.errors.details) end + test "slice! removes all errors except the given keys" do + person = Person.new + person.errors.add(:name, "cannot be nil") + person.errors.add(:age, "cannot be nil") + person.errors.add(:gender, "cannot be nil") + person.errors.add(:city, "cannot be nil") + + person.errors.slice!(:age, "gender") + + assert_equal [:age, :gender], person.errors.keys + end + + test "slice! returns the deleted errors" do + person = Person.new + person.errors.add(:name, "cannot be nil") + person.errors.add(:age, "cannot be nil") + person.errors.add(:gender, "cannot be nil") + person.errors.add(:city, "cannot be nil") + + removed_errors = person.errors.slice!(:age, "gender") + + assert_equal({ name: ["cannot be nil"], city: ["cannot be nil"] }, removed_errors) + end + test "errors are marshalable" do errors = ActiveModel::Errors.new(Person.new) errors.add(:name, :invalid) |