Sha256: e16048ffdb994c9d808395361293a1e8d05415aba3c99ff140da3ba686d62326

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

require 'test_helper'

module ActiveModel
  class ArraySerializer
    class SerializeTest < ActiveRecord::TestCase
      def setup
        array = [1, 2, 3]
        @serializer = ActiveModel::Serializer.serializer_for(array).new(array)
      end

      def test_serializer_for_array_returns_appropriate_type
        assert_kind_of ArraySerializer, @serializer
      end

      def test_array_serializer_serializes_simple_objects
        assert_equal [1, 2, 3], @serializer.serializable_array
        assert_equal [1, 2, 3], @serializer.as_json
      end

      def test_array_serializer_serializes_models
        array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
                 Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
        serializer = ArraySerializer.new(array)

        expected = [{ name: 'Name 1', description: 'Description 1' },
                    { name: 'Name 2', description: 'Description 2' }]

        assert_equal expected, serializer.serializable_array
        assert_equal expected, serializer.as_json
      end

      def test_array_serializers_each_serializer
        array = [::Model.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
                 ::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
        serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer)

        expected = [{ name: 'Name 1', description: 'Description 1' },
                    { name: 'Name 2', description: 'Description 2' }]

        assert_equal expected, serializer.serializable_array
        assert_equal expected, serializer.as_json
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_model_serializers_rails_2.3-0.9.0.pre2 test/unit/active_model/array_serializer/serialize_test.rb
active_model_serializers_rails_2.3-0.9.0.pre1 test/unit/active_model/array_serializer/serialize_test.rb