Sha256: cfc33f48b92ec84cafac5fba7c46daf9d4f0d2adde84865ef2dcc75465e4e10f

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

require 'spec_helper'

describe GtinValidator do
  subject { described_class.new(attributes: 'gtin') }

  let(:record) { instance_double('model') }

  before do
    allow(record).to receive(:errors).and_return([])
    allow(record.errors).to receive(:messages).and_return({})
    allow(record.errors).to receive(:add) do |attribute, error|
      record.errors.messages[attribute] = [error]
    end
  end

  context 'when GTIN code is valid' do
    before { subject.validate_each(record, 'gtin', '1243658721548') }

    it "doesn't add errors in model" do
      expect(record.errors.messages).to be_empty
    end
  end

  context 'when GTIN is blank' do
    before { subject.validate_each(record, 'gtin', '') }

    it "doesn't add errors in model" do
      expect(record.errors.messages).to be_empty
    end
  end

  context 'when GTIN is nil' do
    before { subject.validate_each(record, 'gtin', nil) }

    it "doesn't add errors in model" do
      expect(record.errors.messages).to be_empty
    end
  end

  context 'when GTIN is invalid' do
    before { subject.validate_each(record, 'gtin', '253667853') }

    it 'adds errors in model' do
      expect(record.errors.messages).to include 'gtin' => [:invalid]
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
br_documents-0.2.5 spec/br_documents/gtin_validator_spec.rb
br_documents-0.2.4 spec/br_documents/gtin_validator_spec.rb
br_documents-0.2.3 spec/br_documents/gtin_validator_spec.rb
br_documents-0.2.2 spec/br_documents/gtin_validator_spec.rb
br_documents-0.2.1 spec/br_documents/gtin_validator_spec.rb