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

strategy_in_callback_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: 9dcfebc7c1dae78adecd78574e2c4958dd4e88b8 (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
# frozen_string_literal: true

require 'rubocop_spec_helper'

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

RSpec.describe RuboCop::Cop::RSpec::FactoryBot::StrategyInCallback do
  shared_examples 'an offensive factory call' do |namespace|
    described_class::FORBIDDEN_METHODS.each do |forbidden_method|
      namespaced_forbidden_method = "#{namespace}#{forbidden_method}(:ci_job_artifact, :archive)"

      it "registers an offence for multiple #{namespaced_forbidden_method} calls" do
        expect_offense(<<-RUBY)
        FactoryBot.define do
          factory :ci_build, class: 'Ci::Build', parent: :ci_processable do
            trait :artifacts do
              before(:create) do
                #{namespaced_forbidden_method}
                #{'^' * namespaced_forbidden_method.size} Prefer inline `association` over `#{forbidden_method}`. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
              end

              after(:create) do |build|
                #{namespaced_forbidden_method}
                #{'^' * namespaced_forbidden_method.size} Prefer inline `association` over `#{forbidden_method}`. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
                #{namespaced_forbidden_method}
                #{'^' * namespaced_forbidden_method.size} Prefer inline `association` over `#{forbidden_method}`. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
              end
            end
          end
        end
        RUBY
      end

      it "registers an offense for #{namespaced_forbidden_method} when is a send node" do
        expect_offense(<<-RUBY)
        FactoryBot.define do
          factory :ci_build, class: 'Ci::Build', parent: :ci_processable do
            trait :artifacts do
              after(:create) do |build|
                #{namespaced_forbidden_method}
                #{'^' * namespaced_forbidden_method.size} Prefer inline `association` over `#{forbidden_method}`. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories
              end
            end
          end
        end
        RUBY
      end

      it "registers an offense for #{namespaced_forbidden_method} when is assigned" do
        expect_offense(<<-RUBY)
        FactoryBot.define do
          factory :ci_build, class: 'Ci::Build', parent: :ci_processable do
            trait :artifacts do
              after(:create) do |build|
                ci_build = #{namespaced_forbidden_method}
                           #{'^' * namespaced_forbidden_method.size} Prefer inline `association` over `#{forbidden_method}`. See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#factories

                ci_build
              end
            end
          end
        end
        RUBY
      end
    end
  end

  it_behaves_like 'an offensive factory call', ''
  it_behaves_like 'an offensive factory call', 'FactoryBot.'
  it_behaves_like 'an offensive factory call', '::FactoryBot.'
end