summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/rspec/factory_bot/local_static_assignment_spec.rb
blob: de86435616cdec0b401262692304b6d12f424093 (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
# frozen_string_literal: true

require 'rubocop_spec_helper'

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

RSpec.describe RuboCop::Cop::RSpec::FactoryBot::LocalStaticAssignment, feature_category: :tooling do
  shared_examples 'local static assignment' do |block|
    it "flags static local assignment in `#{block}`" do
      expect_offense(<<~RUBY, block: block)
        %{block} do
          age
          name

          random_number = rand(23)
          ^^^^^^^^^^^^^^^^^^^^^^^^ Avoid local static assignemnts in factories which lead to static data definitions.

          random_string = SecureRandom.uuid
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid local static assignemnts in factories which lead to static data definitions.

          project
        end
      RUBY
    end

    it 'does not flag correct use' do
      expect_no_offenses(<<~RUBY)
        #{block} do
          age do
            random_number = rand(23)
            random_number + 1
          end
        end
      RUBY
    end
  end

  it_behaves_like 'local static assignment', 'factory :project'
  it_behaves_like 'local static assignment', 'transient'
  it_behaves_like 'local static assignment', 'trait :closed'

  it 'does not flag local assignments in unrelated blocks' do
    expect_no_offenses(<<~RUBY)
      factory :project do
        sequence(:number) do |n|
          random_number = rand(23)
          random_number * n
        end

        name do
          random_string = SecureRandom.uuid
          random_string + "-name"
        end

        initialize_with do
          random_string = SecureRandom.uuid
          new(name: random_string)
        end
      end
    RUBY
  end
end