From 6b4c8df6fb6bc142ee6a74da51bb26c42a025b3c Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 6 Mar 2016 23:33:27 -0600 Subject: [PATCH] Clean up test deprecation warnings --- lib/active_model/serializer.rb | 2 +- test/adapter/deprecation_test.rb | 63 ++++++++++++++------------------ 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 003a61bf..d17e1879 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -19,6 +19,7 @@ require 'active_model/serializer/type' module ActiveModel class Serializer extend ActiveSupport::Autoload + autoload :Adapter include Configuration include Associations include Attributes @@ -26,7 +27,6 @@ module ActiveModel include Links include Meta include Type - autoload :Adapter # @param resource [ActiveRecord::Base, ActiveModelSerializers::Model] # @return [ActiveModel::Serializer] diff --git a/test/adapter/deprecation_test.rb b/test/adapter/deprecation_test.rb index f33da454..ea858caa 100644 --- a/test/adapter/deprecation_test.rb +++ b/test/adapter/deprecation_test.rb @@ -3,23 +3,29 @@ module ActiveModel class Serializer module Adapter class DeprecationTest < ActiveSupport::TestCase - class DeprecatedPostSerializer < ActiveModel::Serializer + class PostSerializer < ActiveModel::Serializer attribute :body end setup do post = Post.new(id: 1, body: 'Hello') - @serializer = DeprecatedPostSerializer.new(post) + @serializer = PostSerializer.new(post) end - def test_null_adapter_serialization - assert_equal({}, Null.new(@serializer).as_json) + def test_null_adapter_serialization_deprecation + expected = {} + assert_deprecated do + assert_equal(expected, Null.new(@serializer).as_json) + end end - def test_json_adapter_serialization - assert_equal({ post: { body: 'Hello' } }, Json.new(@serializer).as_json) + def test_json_adapter_serialization_deprecation + expected = { post: { body: 'Hello' } } + assert_deprecated do + assert_equal(expected, Json.new(@serializer).as_json) + end end - def test_jsonapi_adapter_serialization + def test_jsonapi_adapter_serialization_deprecation expected = { data: { id: '1', @@ -29,27 +35,16 @@ module ActiveModel } } } - assert_equal(expected, JsonApi.new(@serializer).as_json) + assert_deprecated do + assert_equal(expected, JsonApi.new(@serializer).as_json) + end end - def test_attributes_adapter_serialization - assert_equal({ body: 'Hello' }, Attributes.new(@serializer).as_json) - end - - def test_null_adapter_deprecation - assert_deprecated_adapter(Null) - end - - def test_json_adapter_deprecation - assert_deprecated_adapter(Json) - end - - def test_json_api_adapter_deprecation - assert_deprecated_adapter(JsonApi) - end - - def test_attributes_adapter_deprecation - assert_deprecated_adapter(Attributes) + def test_attributes_adapter_serialization_deprecation + expected = { body: 'Hello' } + assert_deprecated do + assert_equal(expected, Attributes.new(@serializer).as_json) + end end def test_adapter_create_deprecation @@ -78,8 +73,11 @@ module ActiveModel def test_adapter_register_deprecation assert_deprecated do - Adapter.register(:test, Class.new) - Adapter.adapter_map.delete('test') + begin + Adapter.register(:test, Class.new) + ensure + Adapter.adapter_map.delete('test') + end end end @@ -91,15 +89,8 @@ module ActiveModel private - def assert_deprecated_adapter(adapter) - assert_deprecated do - adapter.new(@serializer) - end - end - def assert_deprecated - message = /deprecated/ - assert_output(nil, message) do + assert_output(nil, /deprecated/) do yield end end