Sha256: b77bd5353b8fb4fc62025f2a5ebcd340c26fca8a93fa8e0fb55b38c7fa0fbe08

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require 'spec_helper'

describe SimpleCommand::Errors do
  let(:errors) { SimpleCommand::Errors.new }

  describe '#add' do
    before do
      errors.add :some_error, 'some error description'
    end

    it 'adds the error' do
      expect(errors[:some_error]).to eq(['some error description'])
    end

    it 'adds the same error only once' do
      errors.add :some_error, 'some error description'
      expect(errors[:some_error]).to eq(['some error description'])
    end
  end

  describe '#add_multiple_errors' do
    let(:errors_list) do
      {
        some_error: ['some error description'],
        another_error: ['another error description']
      }
    end

    before do
      errors.add_multiple_errors errors_list
    end

    it 'populates itself with the added errors' do
      expect(errors[:some_error]).to eq(errors_list[:some_error])
      expect(errors[:another_error]).to eq(errors_list[:another_error])
    end
  end

  describe '#each' do
    let(:errors_list) do
      {
        email: ['taken'],
        password: ['blank', 'too short']
      }
    end

    before { errors.add_multiple_errors(errors_list) }

    it 'yields each message for the same key independently' do
      expect { |b| errors.each(&b) }.to yield_control.exactly(3).times
      expect { |b| errors.each(&b) }.to yield_successive_args(
        [:email, 'taken'],
        [:password, 'blank'],
        [:password, 'too short']
      )
    end
  end

  describe '#full_messages' do
    before do
      errors.add :attr1, 'has an error'
      errors.add :attr2, 'has an error'
      errors.add :attr2, 'has two errors'
    end

    it "returrns the full messages array" do
      expect(errors.full_messages).to eq ["Attr1 has an error", "Attr2 has an error", "Attr2 has two errors"]
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
simple_command-0.2.0 spec/simple_command/errors_spec.rb
simple_command-0.1.0 spec/simple_command/errors_spec.rb