Sha256: a64cb0710fe8d6c1502b17538e72a2e268b8bee3ea87e3b401f3b59035e8e356

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

require 'test_helper'

module ActionController
  module Serialization
    class AdapterSelectorTest < ActionController::TestCase
      class Profile < Model
        attributes :id, :name, :description
        associations :comments
      end
      class ProfileSerializer < ActiveModel::Serializer
        type 'profiles'
        attributes :name, :description
      end

      class AdapterSelectorTestController < ActionController::Base
        def render_using_default_adapter
          @profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
          render json: @profile
        end

        def render_using_adapter_override
          @profile = Profile.new(id: 'render_using_adapter_override', name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
          render json: @profile, adapter: :json_api
        end

        def render_skipping_adapter
          @profile = Profile.new(id: 'render_skipping_adapter_id', name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
          render json: @profile, adapter: false
        end
      end

      tests AdapterSelectorTestController

      def test_render_using_default_adapter
        get :render_using_default_adapter
        assert_equal '{"name":"Name 1","description":"Description 1"}', response.body
      end

      def test_render_using_adapter_override
        get :render_using_adapter_override

        expected = {
          data: {
            id: 'render_using_adapter_override',
            type: 'profiles',
            attributes: {
              name: 'Name 1',
              description: 'Description 1'
            }
          }
        }

        assert_equal expected.to_json, response.body
      end

      def test_render_skipping_adapter
        get :render_skipping_adapter
        assert_equal '{"id":"render_skipping_adapter_id","name":"Name 1","description":"Description 1"}', response.body
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_model_serializers-0.10.8 test/action_controller/adapter_selector_test.rb
active_model_serializers-0.10.7 test/action_controller/adapter_selector_test.rb
active_model_serializers-0.10.6 test/action_controller/adapter_selector_test.rb