Welcome to mirror list, hosted at ThFree Co, Russian Federation.

inline_association_spec.rb « factory_bot « rspec « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70dbe086127fc468e924df45cde41fde2203aa43 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'
require 'rubocop'

require_relative '../../../../../rubocop/cop/rspec/factory_bot/inline_association'

RSpec.describe RuboCop::Cop::RSpec::FactoryBot::InlineAssociation, type: :rubocop do
  include CopHelper

  subject(:cop) { described_class.new }

  shared_examples 'offense' do |code_snippet, autocorrected|
    # We allow `create` or `FactoryBot.create` or `::FactoryBot.create`
    let(:type) { code_snippet[/^(?:::)?(?:FactoryBot\.)?(\w+)/, 1] }
    let(:offense_marker) { '^' * code_snippet.size }
    let(:offense_msg) { msg(type) }
    let(:offense) { "#{offense_marker} #{offense_msg}" }
    let(:pristine_source) { source.sub(offense, '') }
    let(:source) do
      <<~RUBY
          FactoryBot.define do
            factory :project do
              attribute { #{code_snippet} }
                          #{offense}
            end
          end
      RUBY
    end

    it 'registers an offense' do
      expect_offense(source)
    end

    it 'autocorrects the source' do
      corrected = autocorrect_source(pristine_source)

      expect(corrected).not_to include(code_snippet)
      expect(corrected).to include(autocorrected)
    end
  end

  shared_examples 'no offense' do |code_snippet|
    first_line = code_snippet.lines.first.chomp

    context "for `#{first_line}`" do
      it 'does not register any offenses' do
        expect_no_offenses <<~RUBY
          FactoryBot.define do
            factory :project do
              #{code_snippet}
            end
          end
        RUBY
      end
    end
  end

  context 'offenses' do
    using RSpec::Parameterized::TableSyntax

    where(:code_snippet, :autocorrected) do
      # create
      'create(:user)'              | 'association(:user)'
      'FactoryBot.create(:user)'   | 'association(:user)'
      '::FactoryBot.create(:user)' | 'association(:user)'
      'create(:user, :admin)'      | 'association(:user, :admin)'
      'create(:user, name: "any")' | 'association(:user, name: "any")'
      # build
      'build(:user)'               | 'association(:user)'
      'FactoryBot.build(:user)'    | 'association(:user)'
      '::FactoryBot.build(:user)'  | 'association(:user)'
      'build(:user, :admin)'       | 'association(:user, :admin)'
      'build(:user, name: "any")'  | 'association(:user, name: "any")'
    end

    with_them do
      include_examples 'offense', params[:code_snippet], params[:autocorrected]
    end

    it 'recognizes `add_attribute`' do
      expect_offense <<~RUBY
        FactoryBot.define do
          factory :project, class: 'Project' do
            add_attribute(:method) { create(:user) }
                                     ^^^^^^^^^^^^^ #{msg(:create)}
          end
        end
      RUBY
    end

    it 'recognizes `transient` attributes' do
      expect_offense <<~RUBY
        FactoryBot.define do
          factory :project, class: 'Project' do
            transient do
              creator { create(:user) }
                        ^^^^^^^^^^^^^ #{msg(:create)}
            end
          end
        end
      RUBY
    end
  end

  context 'no offenses' do
    include_examples 'no offense', 'association(:user)'
    include_examples 'no offense', 'association(:user, :admin)'
    include_examples 'no offense', 'association(:user, name: "any")'

    include_examples 'no offense', <<~RUBY
      after(:build) do |object|
        object.user = create(:user)
      end
    RUBY

    include_examples 'no offense', <<~RUBY
      initialize_with do
        create(:user)
      end
    RUBY

    include_examples 'no offense', <<~RUBY
      user_id { create(:user).id }
    RUBY
  end

  def msg(type)
    format(described_class::MSG, type: type)
  end
end