diff options
author | Thilo Utke <thilo@upstre.am> | 2010-11-28 14:36:40 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-12-01 11:43:37 +0100 |
commit | 7148b933c4865a5140187d7ed792fd6df9b860a4 (patch) | |
tree | 56f77db136192f65eb4e3a1417d8b989e56cfe37 /activemodel/test/cases | |
parent | 1ec126dd37b52ecf7c0c24a842fc87836d8f2e9b (diff) | |
download | rails-7148b933c4865a5140187d7ed792fd6df9b860a4.tar.gz rails-7148b933c4865a5140187d7ed792fd6df9b860a4.tar.bz2 rails-7148b933c4865a5140187d7ed792fd6df9b860a4.zip |
ActiveModel::Errors.to_hash returns plain OrderedHash and used in to_json serialization to properly handle multiple errors per attribute [#5615 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activemodel/test/cases')
-rw-r--r-- | activemodel/test/cases/errors_test.rb | 5 | ||||
-rw-r--r-- | activemodel/test/cases/serializeration/json_serialization_test.rb | 15 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 4 |
3 files changed, 15 insertions, 9 deletions
diff --git a/activemodel/test/cases/errors_test.rb b/activemodel/test/cases/errors_test.rb index 79b45bb298..27821c333b 100644 --- a/activemodel/test/cases/errors_test.rb +++ b/activemodel/test/cases/errors_test.rb @@ -62,4 +62,9 @@ class ErrorsTest < ActiveModel::TestCase end + test 'to_hash should return an ordered hash' do + person = Person.new + person.errors.add(:name, "can not be blank") + assert_instance_of ActiveSupport::OrderedHash, person.errors.to_hash + end end diff --git a/activemodel/test/cases/serializeration/json_serialization_test.rb b/activemodel/test/cases/serializeration/json_serialization_test.rb index 20d123ef0b..500a5c575f 100644 --- a/activemodel/test/cases/serializeration/json_serialization_test.rb +++ b/activemodel/test/cases/serializeration/json_serialization_test.rb @@ -6,6 +6,7 @@ require 'active_support/core_ext/object/instance_variables' class Contact extend ActiveModel::Naming include ActiveModel::Serializers::JSON + include ActiveModel::Validations def attributes instance_values @@ -105,15 +106,15 @@ class JsonSerializationTest < ActiveModel::TestCase end test "should return OrderedHash for errors" do - car = Automobile.new - - # run the validation - car.valid? + contact = Contact.new + contact.errors.add :name, "can't be blank" + contact.errors.add :name, "is too short (minimum is 2 characters)" + contact.errors.add :age, "must be 16 or over" hash = ActiveSupport::OrderedHash.new - hash[:make] = "can't be blank" - hash[:model] = "is too short (minimum is 2 characters)" - assert_equal hash.to_json, car.errors.to_json + hash[:name] = ["can't be blank", "is too short (minimum is 2 characters)"] + hash[:age] = ["must be 16 or over"] + assert_equal hash.to_json, contact.errors.to_json end test "serializable_hash should not modify options passed in argument" do diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 4024002aaa..55b477dd10 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -174,8 +174,8 @@ class ValidationsTest < ActiveModel::TestCase assert_match %r{<error>Content can't be blank</error>}, xml hash = ActiveSupport::OrderedHash.new - hash[:title] = "can't be blank" - hash[:content] = "can't be blank" + hash[:title] = ["can't be blank"] + hash[:content] = ["can't be blank"] assert_equal t.errors.to_json, hash.to_json end |