active_model_serializers/test/adapter_test.rb
Benjamin Fleischer 19de5f7722 Introduce Adapter::Base
Breaking change:
- Adapters now inherit Adapter::Base
- 'Adapter' is now a module, no longer a class
Why?

- using a class as a namespace that you also inherit from is complicated and circular at time i.e.
  buggy (see https://github.com/rails-api/active_model_serializers/pull/1177)
- The class methods on Adapter aren't necessarily related to the instance methods, they're more
    Adapter functions
- named `Base` because it's a Rails-ism
- It helps to isolate and highlight what the Adapter interface actually is
2015-09-20 12:26:04 -05:00

43 lines
1.3 KiB
Ruby

require 'test_helper'
module ActiveModel
class Serializer
class AdapterTest < Minitest::Test
def setup
profile = Profile.new
@serializer = ProfileSerializer.new(profile)
@adapter = ActiveModel::Serializer::Adapter::Base.new(@serializer)
end
def test_serializable_hash_is_abstract_method
assert_raises(NotImplementedError) do
@adapter.serializable_hash(only: [:name])
end
end
def test_serializer
assert_equal @serializer, @adapter.serializer
end
def test_create_adapter
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
assert_equal ActiveModel::Serializer::Adapter::Attributes, adapter.class
end
def test_create_adapter_with_override
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api })
assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
end
def test_inflected_adapter_class_for_known_adapter
ActiveSupport::Inflector.inflections(:en) { |inflect| inflect.acronym 'API' }
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
ActiveSupport::Inflector.inflections.acronyms.clear
assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass
end
end
end
end