summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/config/entry/validatable_spec.rb
blob: 925db3594ba057844d295c4bf103d3cadb071fba (plain)
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
26
27
28
29
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
55
56
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Config::Entry::Validatable do
  let(:entry) do
    Class.new(Gitlab::Config::Entry::Node) do
      include Gitlab::Config::Entry::Validatable
    end
  end

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

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

    it 'returns validator' do
      expect(entry.validator.superclass)
        .to be Gitlab::Config::Entry::Validator
    end

    it 'returns only one validator to mitigate leaks' do
      expect { entry.validator }.not_to change { entry.validator }
    end

    context 'when validating entry instance' do
      let(:entry_instance) { entry.new('something') }

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

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

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

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