summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/gitlab/finder_with_find_by_spec.rb
blob: db3bcf1dfdb9292340ada58d07d96f1d7447e24a (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
57
58
59
# frozen_string_literal: true

require 'fast_spec_helper'

require 'rubocop'
require 'rubocop/rspec/support'

require_relative '../../../../rubocop/cop/gitlab/finder_with_find_by'

RSpec.describe RuboCop::Cop::Gitlab::FinderWithFindBy do
  include CopHelper

  subject(:cop) { described_class.new }

  context 'when calling execute.find' do
    let(:source) do
      <<~SRC
      DummyFinder.new(some_args)
        .execute
        .find_by!(1)
      SRC
    end

    let(:corrected_source) do
      <<~SRC
      DummyFinder.new(some_args)
        .find_by!(1)
      SRC
    end

    it 'registers an offence' do
      inspect_source(source)

      expect(cop.offenses.size).to eq(1)
    end

    it 'can autocorrect the source' do
      expect(autocorrect_source(source)).to eq(corrected_source)
    end

    context 'when called within the `FinderMethods` module' do
      let(:source) do
        <<~SRC
          module FinderMethods
            def find_by!(*args)
              execute.find_by!(args)
            end
          end
        SRC
      end

      it 'does not register an offence' do
        inspect_source(source)

        expect(cop.offenses).to be_empty
      end
    end
  end
end