Sha256: 01ed2d0222fcbcaebfa17a36d0fced8cfaa5f539119c37cd0b806aa9d78a0aa5

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

require 'spec_helper'

describe Virtus::AttributeSet, '#[]=' do
  subject { object[name] = attribute }

  let(:attributes) { []                                      }
  let(:parent)     { described_class.new                     }
  let(:object)     { described_class.new(parent, attributes) }
  let(:name)       { :name                                   }

  context 'with a new attribute' do
    let(:attribute) { mock('attribute', :name => name) }

    it { should equal(attribute) }

    it 'adds an attribute' do
      expect { subject }.to change { object.to_a }.from(attributes).to([ attribute ])
    end

    it 'allows #[] to access the attribute with a symbol' do
      expect { subject }.to change { object['name'] }.from(nil).to(attribute)
    end

    it 'allows #[] to access the attribute with a string' do
      expect { subject }.to change { object[:name] }.from(nil).to(attribute)
    end

    it 'allows #reset to track overridden attributes' do
      expect { subject }.to change { object.reset.to_a }.from(attributes).to([ attribute ])
    end
  end

  context 'with a duplicate attribute' do
    let(:attributes) { [ mock('original', :name => name) ] }
    let(:attribute)  { mock('attribute', :name => name)    }

    it { should equal(attribute) }

    it 'replaces the original attribute' do
      expect { subject }.to change { object.to_a }.from(attributes).to([ attribute ])
    end

    it 'allows #[] to access the attribute with a symbol' do
      expect { subject }.to change { object['name'] }.from(nil).to(attribute)
    end

    it 'allows #[] to access the attribute with a string' do
      expect { subject }.to change { object[:name] }.from(nil).to(attribute)
    end

    it 'allows #reset to track overridden attributes' do
      expect { subject }.to change { object.reset.to_a }.from(attributes).to([ attribute ])
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
virtus-0.3.0 spec/unit/virtus/attribute_set/element_set_spec.rb
virtus-0.2.0 spec/unit/virtus/attribute_set/element_set_spec.rb
virtus-0.1.0 spec/unit/virtus/attribute_set/element_set_spec.rb