Add some failing tests around has_many assocs...

..where no serializer is defined for the thing that is has_many'd
This commit is contained in:
Justin Aiken 2015-06-12 15:05:33 -06:00 committed by João Moura
parent 189b79523c
commit 3710c32cee
4 changed files with 31 additions and 1 deletions

View File

@ -17,6 +17,8 @@ module ActiveModel
@second_comment.post = @post
@blog = Blog.new(id: 1, name: "My Blog!!")
@post.blog = @blog
@tag = Tag.new(id: 1, name: "#hash_tag")
@post.tags = [@tag]
@serializer = PostSerializer.new(@post)
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
@ -28,9 +30,14 @@ module ActiveModel
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
], @adapter.serializable_hash[:post][:comments])
end
def test_has_many_with_no_serializer
serializer = PostWithTagsSerializer.new(@post)
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
assert_includes(adapter.as_json, :tags)
end
end
end
end
end
end

View File

@ -27,6 +27,8 @@ module ActiveModel
@blog.articles = [@post]
@post.blog = @blog
@post_without_comments.blog = nil
@tag = Tag.new(id: 1, name: "#hash_tag")
@post.tags = [@tag]
@serializer = PostSerializer.new(@post)
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
@ -103,6 +105,12 @@ module ActiveModel
}
assert_equal expected, actual
end
def test_has_many_with_no_serializer
serializer = PostWithTagsSerializer.new(@post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
assert_includes(adapter.serializable_hash, :tags)
end
end
end
end

View File

@ -76,6 +76,7 @@ Role = Class.new(Model)
User = Class.new(Model)
Location = Class.new(Model)
Place = Class.new(Model)
Tag = Class.new(Model)
Comment = Class.new(Model) do
# Uses a custom non-time-based cache key
def cache_key
@ -224,6 +225,12 @@ PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
belongs_to :author, serializer: AuthorPreviewSerializer
end
PostWithTagsSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
has_many :tags
end
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
end

View File

@ -29,8 +29,10 @@ module ActiveModel
@author.roles = []
@blog = Blog.new({ name: 'AMS Blog' })
@post = Post.new({ title: 'New Post', body: 'Body' })
@tag = Tag.new({name: '#hashtagged'})
@comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
@post.comments = [@comment]
@post.tags = [@tag]
@post.blog = @blog
@comment.post = @post
@comment.author = nil
@ -65,6 +67,12 @@ module ActiveModel
end
end
def test_has_many_with_no_serializer
PostWithTagsSerializer.new(@post).each_association do |name, serializer, options|
puts "The line above will crash this test"
end
end
def test_serializer_options_are_passed_into_associations_serializers
@post_serializer.each_association do |name, association|
if name == :comments