Sha256: b0f6c16e40e46926caa3879113cde5b1bade2669d53c0c803cdd5a317ef3343c

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

require 'spec_helper'
require 'observed/configurable'

module ConfigurableSpec
  class Foo
    include Observed::Configurable

    attribute :foo, default: 123
    attribute :bar, default: 234
    attribute :baz

    default :bar => 345
  end
end

describe Observed::Configurable do

  context 'without parameters for the constructor' do
    subject {
      ConfigurableSpec::Foo.new
    }
    it 'uses default values for attributes' do
      expect(subject.foo).to eq(123)
    end
    it 'overrides default values on `attribute name, :default => default_value`' do
      expect(subject.bar).to eq(345)
    end
    it 'raises errors when attributes without values are read' do
      expect { subject.baz }.to raise_error
    end
  end

  context 'with parameters for the constructor' do
    subject {
      ConfigurableSpec::Foo.new({foo: 1, bar: 2, baz: 3})
    }
    it 'prefers values from constructor parameters over defaults' do
      expect(subject.foo).to eq(1)
      expect(subject.bar).to eq(2)
      expect(subject.baz).to eq(3)
    end
  end

  context 'configured through `configure(args)` method' do
    subject {
      foo = ConfigurableSpec::Foo.new
      foo.configure(args)
      foo
    }
    shared_examples_for 'values are set' do
      it 'prefers values from `configure(args)` over defaults' do
        expect(subject.foo).to eq(1)
        expect(subject.bar).to eq(2)
        expect(subject.baz).to eq(3)
      end
    end
    context 'when args has symbol keys' do
      let(:args) {
        {foo: 1, bar: 2, baz: 3}
      }
      it_behaves_like 'values are set'
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
observed-0.1.1 spec/configurable_spec.rb