Sha256: 3379e99dc4da738b0e74ceef949dcb2897c54a69293196aecb294a2b9e942924

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

# encoding: UTF-8
require 'spec_helper'

describe ActiveData::Model::Persistence do
  let(:model) do
    stub_model do
      include ActiveData::Model::Persistence

      attribute :name
      attribute :count, default: 0
    end
  end

  specify { expect(model.new).not_to be_persisted }
  specify { expect(model.new).not_to be_destroyed }

  describe '#instantiate' do
    specify { expect(model.instantiate({})).to be_an_instance_of model }
    specify { expect(model.instantiate({})).to be_persisted }
    specify { expect(model.instantiate({})).not_to be_destroyed }

    context do
      subject(:instance) { model.instantiate(name: 'Hello', foo: 'Bar') }

      specify { expect(subject.instance_variable_get(:@initial_attributes)).to eq({ name: 'Hello' }.stringify_keys) }
    end
  end

  describe '#instantiate_collection' do
    context do
      subject(:instances) { model.instantiate_collection(name: 'Hello', foo: 'Bar') }

      specify { expect(subject).to be_a Array }
      specify { expect(subject.first.instance_variable_get(:@initial_attributes)).to eq({ name: 'Hello' }.stringify_keys) }
    end

    context do
      before { model.send(:include, ActiveData::Model::Scopes) }
      subject(:instances) { model.instantiate_collection([{ name: 'Hello', foo: 'Bar' }, {name: 'World'}]) }

      specify { expect(subject).to be_a ActiveData::Model::Scopes::ScopeProxy }
      specify { expect(subject.count).to eq(2) }
      specify { expect(subject.first.instance_variable_get(:@initial_attributes)).to eq({ name: 'Hello' }.stringify_keys) }
      specify { expect(subject.second.instance_variable_get(:@initial_attributes)).to eq({ name: 'World' }.stringify_keys) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_data-1.0.0 spec/lib/active_data/model/persistence_spec.rb