Sha256: 531cf8af72d294116f3d0ca2f229f0c6bc457c18ebcd593e46f24485725f17dd

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require 'spec_helper'
require 'ditty/services/settings'

describe ::Ditty::Services::Settings do
  def setup_files
    settings = File.read('./spec/fixtures/settings.yml')
    section = File.read('./spec/fixtures/section.yml')

    allow(File).to receive(:file?).and_return(false)
    allow(File).to receive(:file?).with('./config/settings.yml').and_return(true)
    allow(File).to receive(:file?).with('./config/section.yml').and_return(true)

    allow(File).to receive(:read).with('./config/settings.yml').and_return(settings)
    allow(File).to receive(:read).with('./config/section.yml').and_return(section)
  end

  context '#[]' do
    before do
      setup_files
      described_class.values = nil
    end

    it 'returns the specified values from the global settings' do
      expect(described_class[:option_a]).to eq 1
    end

    it 'allows access to sectional settings' do
      expect(described_class[:no_file_section]).to include(section_1: 2, section_2: 'set')
    end

    it 'allows using dots to travers' do
      expect(described_class['nested.option']).to eq 'value'
    end
  end

  context '#values' do
    context 'uses the global file' do
      before do
        setup_files
      end

      it 'to return global settings' do
        expect(described_class.values).to include(option_a: 1, option_b: 'value')
      end

      it 'to return sectional settings' do
        expect(described_class.values(:no_file_section)).to include(section_1: 2, section_2: 'set')
      end
    end

    context 'uses the sectional file' do
      before do
        setup_files
      end

      it 'prefers the sectional settings file' do
        expect(described_class.values(:section)).to include(section_1: 3, section_2: 'section')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ditty-0.9.1 spec/ditty/services/settings_spec.rb