Sha256: 6ffe3565d8113e756f91ea7cf619f081eaab5c5ca9821d575daed8316598b3b9

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

require 'ivy/serializers'

RSpec.describe Ivy::Serializers::Serializer do
  describe '#resource' do
    let(:serializer) { serializer_class.new }
    let(:generator) { Ivy::Serializers::Formats::ActiveModelSerializers.new(document) }
    let(:document) { Ivy::Serializers::Documents.create(serializer, :post, post) }
    let(:post_class) { double('Post') }
    let(:post) { double('post', :class => post_class, :id => 1, :title => 'title') }

    context 'with an attribute' do
      subject { generator.as_json }

      context 'with default options' do
        let(:serializer_class) {
          post_klass = post_class

          Class.new(described_class) do
            map post_klass do
              attribute :title
            end
          end
        }

        it { should eq(:post => {:id => 1, :title => 'title'}) }
      end

      context 'with a block provided' do
        let(:serializer_class) {
          post_klass = post_class

          Class.new(described_class) do
            map post_klass do
              attribute(:title) { title }

              def title
                'custom_title'
              end
            end
          end
        }

        it { should eq(:post => {:id => 1, :title => 'custom_title'}) }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ivy-serializers-0.4.0 spec/integration/serializer_spec.rb
ivy-serializers-0.3.0 spec/integration/serializer_spec.rb
ivy-serializers-0.2.0 spec/integration/serializer_spec.rb
ivy-serializers-0.1.0 spec/integration/serializer_spec.rb