Sha256: b3e3d345fe51562bf1e6619f7362264f51e4bca26c92ac1bd3989992ad68bd0a

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

RSpec.describe Yaks::Mapper::Attribute do
  include_context 'yaks context'

  let(:attribute_with_block) { described_class.create(:the_name) { "Alice" } }

  subject(:attribute) { described_class.create(:the_name) }
  fake(:mapper)

  before do
    stub(mapper).load_attribute(:the_name) { 123 }
    stub(mapper).object { fake(name: "Bob") }
  end

  describe ".create" do
    its(:name) { should be :the_name }
    its(:block) { should be_nil }

    it "should accept two parameter" do
      expect{described_class.create(:the_name, {})}.not_to raise_error()
    end

    context "with block" do
      subject(:attribute) { attribute_with_block }

      its(:block) { should_not be_nil }

      it "should store the given block" do
        expect(subject.block.call).to eq("Alice")
      end
    end
  end

  describe "#add_to_resource" do
    it "should add itself to a resource based on a lookup" do
      expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context))
        .to eql(Yaks::Resource.new(attributes: {the_name: 123}))
    end

    context "when the attribute has a block" do
      subject(:attribute) { attribute_with_block }

      it "should add itself to a resource with the block value" do
        expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context))
          .to eql(Yaks::Resource.new(attributes: {the_name: "Alice"}))
      end

      context "using the mapper context" do
        let(:attribute) { described_class.create(:the_name) { object.name } }

        it "should add itself to a resource with the block value" do
          expect(attribute.add_to_resource(Yaks::Resource.new, mapper, yaks_context))
            .to eql(Yaks::Resource.new(attributes: {the_name: "Bob"}))
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yaks-0.11.0 spec/unit/yaks/mapper/attribute_spec.rb
yaks-0.10.0 spec/unit/yaks/mapper/attribute_spec.rb