Sha256: 045181d3e32f97f7220463c9a1bfdd3a93ca7867dd35ec33e9babb64f7d5dc32

Contents?: true

Size: 1.56 KB

Versions: 15

Compression:

Stored size: 1.56 KB

Contents

require_relative '../spec_helper'

describe RestfulResource::Associations do
  describe '#has_many' do
    before do
      @parent = ComplicatedModule::Parent.new(
        name: 'John Doe',
        other_things: [{ stuff: 'aaa' }, { stuff: 'bbb' }],
        children:
          [
            { first_name: 'David', second_name: 'Doe' },
            { first_name: 'Mary', second_name: 'Doe' }
          ]
      )
    end

    it 'adds a method to access nested resource' do
      expect(@parent.children.first.first_name).to eq 'David'
      expect(@parent.children.last.first_name).to eq 'Mary'
      expect(@parent.children.first.to_json).to eq({ first_name: 'David', second_name: 'Doe' }.to_json)
    end

    it 'picks the right class for the instantiation of chilren' do
      expect(@parent.children.first.full_name).to eq 'David Doe'
    end

    it "uses open object when can't infer class name of association" do
      expect(@parent.other_things.first.stuff).to eq 'aaa'
    end

    it 'returns nil for missing associations' do
      expect(@parent.missing).to be_nil
    end
  end

  describe '#has_one' do
    before do
      @child = ComplicatedModule::Child.new(
        first_name: 'David', second_name: 'Smith',
        parent: { name: 'John Smith' }
      )
    end

    it 'adds a method to access nested resource' do
      expect(@child.parent.name).to eq 'John Smith'
      expect(@child.parent.to_json).to eq({ name: 'John Smith' }.to_json)
    end

    it 'picks the right class for the instantiation of chilren' do
      expect(@child.parent).to be_is_parent
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
restful_resource-2.18.1 spec/restful_resource/associations_spec.rb
restful_resource-2.18.0 spec/restful_resource/associations_spec.rb
restful_resource-2.17.0 spec/restful_resource/associations_spec.rb
restful_resource-2.16.0 spec/restful_resource/associations_spec.rb
restful_resource-2.15.0 spec/restful_resource/associations_spec.rb
restful_resource-2.14.0 spec/restful_resource/associations_spec.rb
restful_resource-2.13.4 spec/restful_resource/associations_spec.rb
restful_resource-2.13.3 spec/restful_resource/associations_spec.rb
restful_resource-2.13.2 spec/restful_resource/associations_spec.rb
restful_resource-2.13.1 spec/restful_resource/associations_spec.rb
restful_resource-2.13.0 spec/restful_resource/associations_spec.rb
restful_resource-2.12.1 spec/restful_resource/associations_spec.rb
restful_resource-2.12.0 spec/restful_resource/associations_spec.rb
restful_resource-2.11.0 spec/restful_resource/associations_spec.rb
restful_resource-2.10.3 spec/restful_resource/associations_spec.rb