Sha256: d0498b2fd383499168f08f6d63f0ab6afe475553deb9a2c10c41ac7b7b03ce42

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

require 'spec_helper'
module Qrb
  describe Attribute, "fetch_on" do

    let(:attr){ Attribute.new(:red, intType) }

    subject{ attr.fetch_on(arg) }

    context 'with an object that does not support fetch' do
      let(:arg){ 12 }

      it 'should raise an error' do
        ->{
          subject
        }.should raise_error(ArgumentError, "Object responding to `fetch` expected")
      end
    end

    context 'with a hash with symbol keys' do
      let(:arg){ { red: 233 } }

      it{ should eq(233) }
    end

    context 'with a hash with string keys' do
      let(:arg){ { "red" => 233 } }

      it{ should eq(233) }
    end

    context 'when the key is missing and no block' do
      let(:arg){ { other: 123 } }

      it 'should raise an error' do
        ->{
          attr.fetch_on(arg)
        }.should raise_error(KeyError)
      end
    end

    context 'when the key is missing and a block' do
      let(:arg){ { other: 123 } }

      it 'should yield the block' do
        attr.fetch_on(arg){ "none" }.should eq("none")
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qrb-0.3.0 spec/unit/attribute/test_fetch_on.rb
qrb-0.2.0 spec/unit/attribute/test_fetch_on.rb
qrb-0.1.0 spec/unit/attribute/test_fetch_on.rb