Sha256: 5de6ddce7cd7b0c5ca94a00ea14c2cebfb484215b2823b33fe79f1dfc1d8d5f9

Contents?: true

Size: 1.73 KB

Versions: 13

Compression:

Stored size: 1.73 KB

Contents

require 'test_helper'

module ActiveModelSerializers
  module Adapter
    class Json
      class BelongsToTest < ActiveSupport::TestCase
        def setup
          @post = Post.new(id: 42, title: 'New Post', body: 'Body')
          @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
          @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
          @post.comments = [@comment]
          @anonymous_post.comments = []
          @comment.post = @post
          @comment.author = nil
          @anonymous_post.author = nil
          @blog = Blog.new(id: 1, name: 'My Blog!!')
          @post.blog = @blog
          @anonymous_post.blog = nil

          @serializer = CommentSerializer.new(@comment)
          @adapter = ActiveModelSerializers::Adapter::Json.new(@serializer)
          ActionController::Base.cache_store.clear
        end

        def test_includes_post
          assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post])
        end

        def test_include_nil_author
          serializer = PostSerializer.new(@anonymous_post)
          adapter = ActiveModelSerializers::Adapter::Json.new(serializer)

          assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash)
        end

        def test_include_nil_author_with_specified_serializer
          serializer = PostPreviewSerializer.new(@anonymous_post)
          adapter = ActiveModelSerializers::Adapter::Json.new(serializer)

          assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
active_model_serializers-0.10.8 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.7 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.6 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.5 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.4 test/adapter/json/belongs_to_test.rb
agi_active_model_serializers-0.10.9 test/adapter/json/belongs_to_test.rb
agi_active_model_serializers-0.10.8 test/adapter/json/belongs_to_test.rb
agi_active_model_serializers-0.10.7 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.3 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.2 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.1 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.0 test/adapter/json/belongs_to_test.rb
active_model_serializers-0.10.0.rc5 test/adapter/json/belongs_to_test.rb