Sha256: 1cad8e39f9fe072bb8bdd7d746f5b206e60900709ebba8253b5cc38bb0c2b836

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'spec_helper'

describe "Using inheritance to share elements and attributes" do

  class Genetics
    include HappyMapper
    content :dna, String
  end

  class Parent
    include HappyMapper
    attribute :love, Integer
    element :genetics, Genetics
  end

  class Child < Parent
    include HappyMapper
    attribute :naivety, String
    has_many :immunities, String
  end

  describe "Child", "a subclass of the Parent" do
    let(:subject) do
      xml = '<child love="99" naivety="trusting"><genetics>ABBA</genetics><immunities>Chicken Pox</immunities></child>'
      Child.parse(xml)
    end

    context "when parsing xml" do
      it 'should be possible to deserialize XML into a Child class instance' do
        expect(subject.love).to eq 99
        expect(subject.genetics.dna).to eq "ABBA"
        expect(subject.naivety).to eq "trusting"
        expect(subject.immunities).to have(1).item
      end
    end

    context "when saving to xml" do
      let(:subject) do
        child = Child.new
        child.love = 100
        child.naivety = 'Bright Eyed'
        child.immunities = [ "Small Pox", "Chicken Pox", "Mumps" ]
        genetics = Genetics.new
        genetics.dna = "GATTACA"
        child.genetics = genetics
        Nokogiri::XML(child.to_xml).root
      end

      it "saves both the Child and Parent attributes" do
        expect(subject.xpath("@naivety").text).to eq "Bright Eyed"
        expect(subject.xpath("@love").text).to eq "100"
      end

      it "saves both the Child and Parent elements" do
        expect(subject.xpath("genetics").text).to eq "GATTACA"
        expect(subject.xpath("immunities")).to have(3).items
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nokogiri-happymapper-0.5.7 spec/inheritance_spec.rb