mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Make serializers serializable, step 1.
This commit is contained in:
55
test/serializers/serialization_test.rb
Normal file
55
test/serializers/serialization_test.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class SerializationTest < ActiveSupport::TestCase
|
||||
class Blog < ActiveModelSerializers::Model
|
||||
attr_accessor :id, :name, :authors
|
||||
end
|
||||
class Author < ActiveModelSerializers::Model
|
||||
attr_accessor :id, :name
|
||||
end
|
||||
class BlogSerializer < ActiveModel::Serializer
|
||||
attributes :id
|
||||
attribute :name, key: :title
|
||||
|
||||
has_many :authors
|
||||
end
|
||||
class AuthorSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name
|
||||
end
|
||||
|
||||
setup do
|
||||
@authors = [Author.new(id: 1, name: 'Blog Author')]
|
||||
@blog = Blog.new(id: 2, name: 'The Blog', authors: @authors)
|
||||
@serializer_instance = BlogSerializer.new(@blog)
|
||||
@serializable = ActiveModel::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes)
|
||||
@expected_hash = { id: 2, title: 'The Blog', authors: [{ id: 1, name: 'Blog Author' }] }
|
||||
@expected_json = '{"id":2,"title":"The Blog","authors":[{"id":1,"name":"Blog Author"}]}'
|
||||
end
|
||||
|
||||
test '#serializable_hash is the same as generated by the attributes adapter' do
|
||||
assert_equal @serializable.serializable_hash, @serializer_instance.serializable_hash
|
||||
assert_equal @expected_hash, @serializer_instance.serializable_hash
|
||||
end
|
||||
|
||||
test '#as_json is the same as generated by the attributes adapter' do
|
||||
assert_equal @serializable.as_json, @serializer_instance.as_json
|
||||
assert_equal @expected_hash, @serializer_instance.as_json
|
||||
end
|
||||
|
||||
test '#to_json is the same as generated by the attributes adapter' do
|
||||
assert_equal @serializable.to_json, @serializer_instance.to_json
|
||||
assert_equal @expected_json, @serializer_instance.to_json
|
||||
end
|
||||
|
||||
test '#to_h is an alias for #serializable_hash' do
|
||||
assert_equal @serializable.serializable_hash, @serializer_instance.to_h
|
||||
assert_equal @expected_hash, @serializer_instance.to_h
|
||||
end
|
||||
|
||||
test '#to_hash is an alias for #serializable_hash' do
|
||||
assert_equal @serializable.serializable_hash, @serializer_instance.to_hash
|
||||
assert_equal @expected_hash, @serializer_instance.to_hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user