Sha256: 7e687c3fe709ad01b134461c56b2c67e52382b5bef053499e76d37ee89da895d

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

require 'spec_helper'

describe Granite::Form::Model::Persistence do
  let(:model) do
    stub_model do
      include Granite::Form::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, Granite::Form::Model::Scopes) }
      subject(:instances) { model.instantiate_collection([{name: 'Hello', foo: 'Bar'}, {name: 'World'}]) }

      specify { expect(subject).to be_a Granite::Form::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

3 entries across 3 versions & 1 rubygems

Version Path
granite-form-0.2.0 spec/granite/form/model/persistence_spec.rb
granite-form-0.1.1 spec/lib/granite/form/model/persistence_spec.rb
granite-form-0.1.0 spec/lib/granite/form/model/persistence_spec.rb