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

env_mocking_spec.rb « rspec « cop « rubocop « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 189fccf483a25a0fb5dd1e39dab1f228dac1d5bb (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
# frozen_string_literal: true

require 'rubocop_spec_helper'

require_relative '../../../../rubocop/cop/rspec/env_mocking'

RSpec.describe RuboCop::Cop::RSpec::EnvMocking, feature_category: :tooling do
  offense_call_brackets_string_quotes = %(allow(ENV).to receive(:[]).with('FOO').and_return('bar'))
  offense_call_brackets_variables = %(allow(ENV).to receive(:[]).with(key).and_return(value))

  offense_call_fetch_string_quotes = %(allow(ENV).to receive(:fetch).with('FOO').and_return('bar'))
  offense_call_fetch_variables = %(allow(ENV).to receive(:fetch).with(key).and_return(value))

  offense_call_root_env_variables = %(allow(::ENV).to receive(:[]).with(key).and_return(value))
  offense_call_key_value_method_calls =
    %(allow(ENV).to receive(:[]).with(fetch_key(object)).and_return(fetch_value(object)))

  acceptable_mocking_other_methods = %(allow(ENV).to receive(:foo).with("key").and_return("value"))

  let(:source_file) { 'spec/foo_spec.rb' }

  shared_examples 'cop offense mocking the ENV constant correctable with stub_env' do |content, autocorrected_content|
    it "registers an offense for `#{content}` and corrects", :aggregate_failures do
      expect_offense(<<~CODE, content: content)
        %{content}
        ^{content} Don't mock the ENV, use `stub_env` instead.
      CODE

      expect_correction(<<~CODE)
        #{autocorrected_content}
      CODE
    end
  end

  context 'with mocking bracket calls ' do
    it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env',
                    offense_call_brackets_string_quotes, %(stub_env('FOO', 'bar'))
    it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env',
                    offense_call_brackets_variables, %(stub_env(key, value))
  end

  context 'with mocking fetch calls' do
    it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env',
                    offense_call_fetch_string_quotes, %(stub_env('FOO', 'bar'))
    it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env',
                    offense_call_fetch_variables, %(stub_env(key, value))
  end

  context 'with other special cases and variations' do
    it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env',
                    offense_call_root_env_variables, %(stub_env(key, value))
    it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env',
                    offense_call_key_value_method_calls, %(stub_env(fetch_key(object), fetch_value(object)))
  end

  context 'with acceptable cases' do
    it 'does not register an offense for mocking other methods' do
      expect_no_offenses(acceptable_mocking_other_methods)
    end
  end
end