validatable_spec.rb 1.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
require 'spec_helper'

describe Gitlab::Ci::Config::Node::Validatable do
  let(:node) { Class.new }

  before do
    node.include(described_class)
  end

  describe '.validator' do
    before do
      node.class_eval do
        attr_accessor :test_attribute

        validations do
          validates :test_attribute, presence: true
        end
      end
    end

    it 'returns validator' do
      expect(node.validator.superclass)
        .to be Gitlab::Ci::Config::Node::Validator
    end

26 27 28 29
    it 'returns only one validator to mitigate leaks' do
      expect { node.validator }.not_to change { node.validator }
    end

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    context 'when validating node instance' do
      let(:node_instance) { node.new }

      context 'when attribute is valid' do
        before do
          node_instance.test_attribute = 'valid'
        end

        it 'instance of validator is valid' do
          expect(node.validator.new(node_instance)).to be_valid
        end
      end

      context 'when attribute is not valid' do
        before do
          node_instance.test_attribute = nil
        end

        it 'instance of validator is invalid' do
          expect(node.validator.new(node_instance)).to be_invalid
        end
      end
    end
  end
end