Sha256: 033556f31e0c0b64390fc0dd200fbff4f6e75bf765b69514f3c0bacf8e95c9b0

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

shared_examples 'settings reading' do
  let(:env_hash) { ENV }

  context 'when the key is not set' do
    it 'retrieves username from env' do
      expect(settable.username).to be_nil
    end

    it 'retrieves password from env' do
      expect(settable.password).to be_nil
    end
  end

  context 'when the key is set' do
    before do
      env_hash[username_key] = username
      env_hash[password_key] = password
    end

    after do
      env_hash.delete(username_key)
      env_hash.delete(password_key)
    end

    it 'retrieves username from env' do
      expect(settable.username).to eq(username)
    end

    it 'retrieves password from env' do
      expect(settable.password).to eq(password)
    end
  end

  context 'when defining defaults' do
    let(:settings)   { %i[host] }
    let(:options)    { { prefix: prefix, default: 'my-host.com' } }

    it 'returns default value' do
      expect(settable.host).to eq('my-host.com')
    end

    context 'when setting the env variable' do
      let(:other_host) { 'other-host.com' }

      before do
        env_hash[host_key] = other_host
      end

      after do
        env_hash.delete(host_key)
      end

      it 'retrieves host from env' do
        expect(settable.host).to eq(other_host)
      end
    end
  end

  context 'when defining a type' do
    let(:settings) { %i[port] }
    let(:options)  { { prefix: prefix, type: :integer } }
    let(:port)     { Random.rand(10..100) }

    context 'when the key is not set' do
      it 'retrieves port and cast to string' do
        expect(settable.port).to be_nil
      end
    end

    context 'when the key is set' do
      before do
        env_hash[port_key] = port.to_s
      end

      after do
        env_hash.delete(port_key)
      end

      it 'retrieves port and cast to string' do
        expect(settable.port).to eq(port)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sinclair-2.1.1 spec/support/shared_examples/settable.rb
sinclair-2.1.0 spec/support/shared_examples/settable.rb