diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-01-30 11:32:38 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-30 11:32:38 -0500 |
commit | 6bf6957137842015c8dff1a2f39a23aec54f41c6 (patch) | |
tree | 0a1355fe5aa40c13e67283be3288ab6638b4433d /activerecord/test/cases | |
parent | 905cececb3dc8e94b1849c36fadba600010926e7 (diff) | |
parent | 99820fbeb29ae35a7771f3991bd119b102861aa7 (diff) | |
download | rails-6bf6957137842015c8dff1a2f39a23aec54f41c6.tar.gz rails-6bf6957137842015c8dff1a2f39a23aec54f41c6.tar.bz2 rails-6bf6957137842015c8dff1a2f39a23aec54f41c6.zip |
Merge pull request #27773 from kirs/serialized-error-attribute
Report the attribute on ActiveRecord::SerializationTypeMismatch
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/coders/yaml_column_test.rb | 24 | ||||
-rw-r--r-- | activerecord/test/cases/serialized_attribute_test.rb | 14 |
2 files changed, 27 insertions, 11 deletions
diff --git a/activerecord/test/cases/coders/yaml_column_test.rb b/activerecord/test/cases/coders/yaml_column_test.rb index b9c6224425..1e0b4580af 100644 --- a/activerecord/test/cases/coders/yaml_column_test.rb +++ b/activerecord/test/cases/coders/yaml_column_test.rb @@ -5,46 +5,48 @@ module ActiveRecord module Coders class YAMLColumnTest < ActiveRecord::TestCase def test_initialize_takes_class - coder = YAMLColumn.new(Object) + coder = YAMLColumn.new("attr_name", Object) assert_equal Object, coder.object_class end def test_type_mismatch_on_different_classes_on_dump - coder = YAMLColumn.new(Array) - assert_raises(SerializationTypeMismatch) do + coder = YAMLColumn.new("attr_name", Array) + error = assert_raises(SerializationTypeMismatch) do coder.dump("a") end + assert_equal %{Attribute `attr_name` was supposed to be a Array, but was a String. -- "a"}, error.to_s end def test_type_mismatch_on_different_classes - coder = YAMLColumn.new(Array) - assert_raises(SerializationTypeMismatch) do + coder = YAMLColumn.new("attr_name", Array) + error = assert_raises(SerializationTypeMismatch) do coder.load "--- foo" end + assert_equal %{Attribute `attr_name` was supposed to be a Array, but was a String. -- "foo"}, error.to_s end def test_nil_is_ok - coder = YAMLColumn.new + coder = YAMLColumn.new("attr_name") assert_nil coder.load "--- " end def test_returns_new_with_different_class - coder = YAMLColumn.new SerializationTypeMismatch + coder = YAMLColumn.new("attr_name", SerializationTypeMismatch) assert_equal SerializationTypeMismatch, coder.load("--- ").class end def test_returns_string_unless_starts_with_dash - coder = YAMLColumn.new + coder = YAMLColumn.new("attr_name") assert_equal "foo", coder.load("foo") end def test_load_handles_other_classes - coder = YAMLColumn.new + coder = YAMLColumn.new("attr_name") assert_equal [], coder.load([]) end def test_load_doesnt_swallow_yaml_exceptions - coder = YAMLColumn.new + coder = YAMLColumn.new("attr_name") bad_yaml = "--- {" assert_raises(Psych::SyntaxError) do coder.load(bad_yaml) @@ -52,7 +54,7 @@ module ActiveRecord end def test_load_doesnt_handle_undefined_class_or_module - coder = YAMLColumn.new + coder = YAMLColumn.new("attr_name") missing_class_yaml = '--- !ruby/object:DoesNotExistAndShouldntEver {}\n' assert_raises(ArgumentError) do coder.load(missing_class_yaml) diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index a469da0a5b..24b83691f6 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -240,6 +240,20 @@ class SerializedAttributeTest < ActiveRecord::TestCase assert_equal [], light.long_state end + def test_unexpected_serialized_type + Topic.serialize :content, Hash + topic = Topic.create!(content: { zomg: true }) + + Topic.serialize :content, Array + + topic.reload + error = assert_raise(ActiveRecord::SerializationTypeMismatch) do + topic.content + end + expected = "Attribute `content` was supposed to be a Array, but was a Hash. -- {:zomg=>true}" + assert_equal expected, error.to_s + end + def test_serialized_column_should_unserialize_after_update_column t = Topic.create(content: "first") assert_equal("first", t.content) |