summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/models/project_ci_cd_settings_shared_examples.rb
blob: 3caf58da4d29259d1ab3572b4bdd36352296c1c4 (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
# frozen_string_literal: true

RSpec.shared_examples 'ci_cd_settings delegation' do
  let(:exclude_attributes) { [] }

  context 'when ci_cd_settings is destroyed but project is not' do
    it 'allows methods delegated to ci_cd_settings to be nil', :aggregate_failures do
      attributes = project.ci_cd_settings.attributes.keys - %w(id project_id) - exclude_attributes

      expect(attributes).to match_array(attributes_with_prefix.keys)

      project.ci_cd_settings.destroy!
      project.reload
      attributes_with_prefix.each do |attr, prefix|
        method = "#{prefix}#{attr}"
        expect(project.send(method)).to be_nil, "#{attr} was not nil"
      end
    end
  end
end

RSpec.shared_examples 'a ci_cd_settings predicate method' do |prefix: ''|
  using RSpec::Parameterized::TableSyntax

  context 'when ci_cd_settings is nil' do
    before do
      allow(project).to receive(:ci_cd_settings).and_return(nil)
    end

    it 'returns false' do
      expect(project.send("#{prefix}#{delegated_method}")).to be(false)
    end
  end

  context 'when ci_cd_settings is not nil' do
    where(:delegated_method_return, :subject_return) do
      true  | true
      false | false
    end

    with_them do
      let(:ci_cd_settings_double) { double('ProjectCiCdSetting') }

      before do
        allow(project).to receive(:ci_cd_settings).and_return(ci_cd_settings_double)
        allow(ci_cd_settings_double).to receive(delegated_method).and_return(delegated_method_return)
      end

      it 'returns the expected boolean value' do
        expect(project.send("#{prefix}#{delegated_method}")).to be(subject_return)
      end
    end
  end
end