Sha256: d7eb2f626c1154206b7044c1b84b3b139541b862275e8477056d632c59981c9a

Contents?: true

Size: 1.85 KB

Versions: 6

Compression:

Stored size: 1.85 KB

Contents

require 'spec_helper'

describe Virtus, '.module' do
  share_examples_for 'a valid virtus object' do
    it 'reads and writes attribute' do
      instance.name = 'Jane'
      expect(instance.name).to eql('Jane')
    end
  end

  share_examples_for 'an object extended with virtus module' do
    context 'with default configuration' do
      subject { Virtus.module }

      it_behaves_like 'a valid virtus object' do
        let(:instance) { model.new }
      end
    end

    context 'with constructor turned off' do
      subject { Virtus.module(:constructor => false) }

      it_behaves_like 'a valid virtus object' do
        let(:instance) { model.new }
      end

      it 'skips including constructor' do
        expect { model.new({}) }.to raise_error(ArgumentError)
      end
    end

    context 'with mass assignment is turned off' do
      subject { Virtus.module(:mass_assignment => false) }

      it_behaves_like 'a valid virtus object'

      it 'skips including mass assignment' do
        expect(instance).not_to respond_to(:attributes)
        expect(instance).not_to respond_to(:attributes=)
      end
    end
  end

  let(:mod)      { Module.new }
  let(:model)    { Class.new }
  let(:instance) { model.new }

  before do
    mod.send(:include, subject)
    mod.attribute :name, String
  end

  context 'with a class' do
    before do
      model.send(:include, mod)
    end

    it 'provides attributes for the model' do
      expect(model.attribute_set[:name]).to be_kind_of(Virtus::Attribute)
    end

    it_behaves_like 'an object extended with virtus module'
  end

  context 'with a model instance' do
    before do
      instance.extend(mod)
    end

    it 'provides attributes for the instance' do
      expect(instance.send(:attribute_set)[:name]).to be_kind_of(Virtus::Attribute)
    end

    it_behaves_like 'an object extended with virtus module'
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
virtus-1.0.0.beta8 spec/unit/virtus/module_spec.rb
virtus-1.0.0.beta7 spec/unit/virtus/module_spec.rb
virtus-1.0.0.beta6 spec/unit/virtus/module_spec.rb
virtus-1.0.0.beta5 spec/unit/virtus/module_spec.rb
virtus-1.0.0.beta4 spec/unit/virtus/module_spec.rb
virtus-1.0.0.beta3 spec/unit/virtus/module_spec.rb