Sha256: 2d43b7c137704dd460d30fc8a8036f5ed31db4e80aa97420bbedb75330cd12e3

Contents?: true

Size: 1.08 KB

Versions: 16

Compression:

Stored size: 1.08 KB

Contents

require 'spec_helper'

describe Virtus, '#attributes=' do

  shared_examples_for 'mass-assignment' do
    it 'allows writing known attributes' do
      subject.attributes = { :test => 'Hello World' }

      expect(subject.test).to eql('Hello World')
    end

    it 'skips writing unknown attributes' do
      subject.attributes = { :test => 'Hello World', :nothere => 'boom!' }

      expect(subject.test).to eql('Hello World')
    end
  end

  context 'with a class' do
    let(:model) {
      Class.new {
        include Virtus

        attribute :test, String
      }
    }

    it_behaves_like 'mass-assignment' do
      subject { model.new }
    end

    it 'raises when attributes is not hash-like object' do
      expect { model.new('not a hash, really') }.to raise_error(
        NoMethodError, 'Expected "not a hash, really" to respond to #to_hash'
      )
    end
  end

  context 'with an instance' do
    subject { model.new }

    let(:model) { Class.new }

    before do
      subject.extend(Virtus)
      subject.attribute :test, String
    end

    it_behaves_like 'mass-assignment'
  end
end

Version data entries

16 entries across 14 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/attributes_writer_spec.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/attributes_writer_spec.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/virtus-2.0.0/spec/unit/virtus/attributes_writer_spec.rb
virtus2-2.1.0 spec/unit/virtus/attributes_writer_spec.rb
virtus2-2.0.2 spec/unit/virtus/attributes_writer_spec.rb
virtus2-2.0.1 spec/unit/virtus/attributes_writer_spec.rb
virtus-2.0.0 spec/unit/virtus/attributes_writer_spec.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/virtus-1.0.5/spec/unit/virtus/attributes_writer_spec.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/virtus-1.0.5/spec/unit/virtus/attributes_writer_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/unit/virtus/attributes_writer_spec.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/unit/virtus/attributes_writer_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/unit/virtus/attributes_writer_spec.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/virtus-1.0.5/spec/unit/virtus/attributes_writer_spec.rb
virtus-1.0.5 spec/unit/virtus/attributes_writer_spec.rb
virtus-1.0.4 spec/unit/virtus/attributes_writer_spec.rb
virtus-1.0.3 spec/unit/virtus/attributes_writer_spec.rb