Sha256: 5f85f6a38338ebbde39ca3ddb32daf9c1186cb536de95f7ac56e67f7ae5bd81c

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

require 'spec_helper'

describe Hashme::CastedArray do

  let :owner do
    double()
  end

  let :submodel do
    Class.new do
      include Hashme
      property :name, String
    end
  end

  describe "#initialize" do
    let :property do
      Hashme::Property.new(:name, String)
    end
    before :each do
      @obj = Hashme::CastedArray.new(property, owner, ['test'])
    end

    it "should prepare array" do
      expect(@obj.length).to eql(1)
    end

    it "should set owner and property" do
      expect(@obj.casted_by).to eql(owner)
      expect(@obj.casted_by_property).to eql(property)
    end

    it "should instantiate and cast each value" do
      expect(@obj.first).to eql("test")
      expect(@obj.first.class).to eql(String)
    end
  end

  describe "adding to array" do
    subject do
      Hashme::CastedArray.new(property, owner, [{:name => 'test'}])
    end
    let :property do
      Hashme::Property.new(:item, submodel)
    end

    it "should cast new items" do
      subject << {:name => 'test2'}
      expect(subject.last.class).to eql(submodel)
      expect(subject.first.name).to eql('test')
      expect(subject.last.name).to eql('test2')

      expect(subject.last.casted_by).to be(subject)
      expect(subject.last.casted_by_property).to eql(property)
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hashme-0.2.3 spec/hashme/casted_array_spec.rb
hashme-0.2.2 spec/hashme/casted_array_spec.rb
hashme-0.2.1 spec/hashme/casted_array_spec.rb
hashme-0.2.0 spec/hashme/casted_array_spec.rb