rubocop-rspec 1.35.0 → 1.36.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/config/default.yml +11 -0
  4. data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +16 -0
  5. data/lib/rubocop/cop/rspec/context_method.rb +53 -0
  6. data/lib/rubocop/cop/rspec/describe_class.rb +7 -10
  7. data/lib/rubocop/cop/rspec/described_class.rb +29 -1
  8. data/lib/rubocop/cop/rspec/empty_line_after_example.rb +87 -0
  9. data/lib/rubocop/cop/rspec/example_wording.rb +28 -12
  10. data/lib/rubocop/cop/rspec/expect_change.rb +8 -8
  11. data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +2 -9
  12. data/lib/rubocop/cop/rspec/focus.rb +5 -10
  13. data/lib/rubocop/cop/rspec/instance_variable.rb +2 -1
  14. data/lib/rubocop/cop/rspec/multiple_expectations.rb +13 -21
  15. data/lib/rubocop/cop/rspec/pending.rb +19 -25
  16. data/lib/rubocop/cop/rspec_cops.rb +2 -0
  17. data/lib/rubocop/rspec/version.rb +1 -1
  18. metadata +7 -198
  19. data/Gemfile +0 -11
  20. data/Rakefile +0 -87
  21. data/rubocop-rspec.gemspec +0 -49
  22. data/spec/project/changelog_spec.rb +0 -76
  23. data/spec/project/default_config_spec.rb +0 -76
  24. data/spec/project/project_requires_spec.rb +0 -20
  25. data/spec/rubocop/cop/rspec/align_left_let_brace_spec.rb +0 -51
  26. data/spec/rubocop/cop/rspec/align_right_let_brace_spec.rb +0 -51
  27. data/spec/rubocop/cop/rspec/any_instance_spec.rb +0 -32
  28. data/spec/rubocop/cop/rspec/around_block_spec.rb +0 -121
  29. data/spec/rubocop/cop/rspec/be_eql_spec.rb +0 -81
  30. data/spec/rubocop/cop/rspec/be_spec.rb +0 -35
  31. data/spec/rubocop/cop/rspec/before_after_all_spec.rb +0 -52
  32. data/spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb +0 -65
  33. data/spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb +0 -130
  34. data/spec/rubocop/cop/rspec/context_wording_spec.rb +0 -94
  35. data/spec/rubocop/cop/rspec/cop_spec.rb +0 -95
  36. data/spec/rubocop/cop/rspec/describe_class_spec.rb +0 -168
  37. data/spec/rubocop/cop/rspec/describe_method_spec.rb +0 -34
  38. data/spec/rubocop/cop/rspec/describe_symbol_spec.rb +0 -44
  39. data/spec/rubocop/cop/rspec/described_class_spec.rb +0 -352
  40. data/spec/rubocop/cop/rspec/dialect_spec.rb +0 -78
  41. data/spec/rubocop/cop/rspec/empty_example_group_spec.rb +0 -93
  42. data/spec/rubocop/cop/rspec/empty_line_after_example_group_spec.rb +0 -121
  43. data/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb +0 -239
  44. data/spec/rubocop/cop/rspec/empty_line_after_hook_spec.rb +0 -133
  45. data/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb +0 -94
  46. data/spec/rubocop/cop/rspec/example_length_spec.rb +0 -78
  47. data/spec/rubocop/cop/rspec/example_without_description_spec.rb +0 -92
  48. data/spec/rubocop/cop/rspec/example_wording_spec.rb +0 -173
  49. data/spec/rubocop/cop/rspec/expect_actual_spec.rb +0 -150
  50. data/spec/rubocop/cop/rspec/expect_change_spec.rb +0 -86
  51. data/spec/rubocop/cop/rspec/expect_in_hook_spec.rb +0 -79
  52. data/spec/rubocop/cop/rspec/expect_output_spec.rb +0 -62
  53. data/spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb +0 -208
  54. data/spec/rubocop/cop/rspec/factory_bot/create_list_spec.rb +0 -171
  55. data/spec/rubocop/cop/rspec/file_path_spec.rb +0 -200
  56. data/spec/rubocop/cop/rspec/focus_spec.rb +0 -139
  57. data/spec/rubocop/cop/rspec/hook_argument_spec.rb +0 -173
  58. data/spec/rubocop/cop/rspec/hooks_before_examples_spec.rb +0 -150
  59. data/spec/rubocop/cop/rspec/implicit_block_expectation_spec.rb +0 -135
  60. data/spec/rubocop/cop/rspec/implicit_expect_spec.rb +0 -102
  61. data/spec/rubocop/cop/rspec/implicit_subject_spec.rb +0 -188
  62. data/spec/rubocop/cop/rspec/instance_spy_spec.rb +0 -61
  63. data/spec/rubocop/cop/rspec/instance_variable_spec.rb +0 -95
  64. data/spec/rubocop/cop/rspec/invalid_predicate_matcher_spec.rb +0 -39
  65. data/spec/rubocop/cop/rspec/it_behaves_like_spec.rb +0 -47
  66. data/spec/rubocop/cop/rspec/iterated_expectation_spec.rb +0 -82
  67. data/spec/rubocop/cop/rspec/leading_subject_spec.rb +0 -119
  68. data/spec/rubocop/cop/rspec/leaky_constant_declaration_spec.rb +0 -91
  69. data/spec/rubocop/cop/rspec/let_before_examples_spec.rb +0 -137
  70. data/spec/rubocop/cop/rspec/let_setup_spec.rb +0 -66
  71. data/spec/rubocop/cop/rspec/message_chain_spec.rb +0 -23
  72. data/spec/rubocop/cop/rspec/message_expectation_spec.rb +0 -45
  73. data/spec/rubocop/cop/rspec/message_spies_spec.rb +0 -151
  74. data/spec/rubocop/cop/rspec/missing_example_group_argument_spec.rb +0 -57
  75. data/spec/rubocop/cop/rspec/multiple_describes_spec.rb +0 -30
  76. data/spec/rubocop/cop/rspec/multiple_expectations_spec.rb +0 -261
  77. data/spec/rubocop/cop/rspec/multiple_subjects_spec.rb +0 -83
  78. data/spec/rubocop/cop/rspec/named_subject_spec.rb +0 -123
  79. data/spec/rubocop/cop/rspec/nested_groups_spec.rb +0 -81
  80. data/spec/rubocop/cop/rspec/not_to_not_spec.rb +0 -77
  81. data/spec/rubocop/cop/rspec/overwriting_setup_spec.rb +0 -91
  82. data/spec/rubocop/cop/rspec/pending_spec.rb +0 -186
  83. data/spec/rubocop/cop/rspec/predicate_matcher_spec.rb +0 -354
  84. data/spec/rubocop/cop/rspec/rails/http_status_spec.rb +0 -90
  85. data/spec/rubocop/cop/rspec/receive_counts_spec.rb +0 -138
  86. data/spec/rubocop/cop/rspec/receive_never_spec.rb +0 -57
  87. data/spec/rubocop/cop/rspec/repeated_description_spec.rb +0 -76
  88. data/spec/rubocop/cop/rspec/repeated_example_spec.rb +0 -74
  89. data/spec/rubocop/cop/rspec/return_from_stub_spec.rb +0 -289
  90. data/spec/rubocop/cop/rspec/scattered_let_spec.rb +0 -28
  91. data/spec/rubocop/cop/rspec/scattered_setup_spec.rb +0 -98
  92. data/spec/rubocop/cop/rspec/shared_context_spec.rb +0 -137
  93. data/spec/rubocop/cop/rspec/shared_examples_spec.rb +0 -75
  94. data/spec/rubocop/cop/rspec/single_argument_message_chain_spec.rb +0 -178
  95. data/spec/rubocop/cop/rspec/subject_stub_spec.rb +0 -293
  96. data/spec/rubocop/cop/rspec/unspecified_exception_spec.rb +0 -177
  97. data/spec/rubocop/cop/rspec/verified_doubles_spec.rb +0 -86
  98. data/spec/rubocop/cop/rspec/void_expect_spec.rb +0 -49
  99. data/spec/rubocop/cop/rspec/yield_spec.rb +0 -81
  100. data/spec/rubocop/rspec/config_formatter_spec.rb +0 -52
  101. data/spec/rubocop/rspec/description_extractor_spec.rb +0 -63
  102. data/spec/rubocop/rspec/example_group_spec.rb +0 -44
  103. data/spec/rubocop/rspec/example_spec.rb +0 -62
  104. data/spec/rubocop/rspec/hook_spec.rb +0 -53
  105. data/spec/rubocop/rspec/language/selector_set_spec.rb +0 -53
  106. data/spec/rubocop/rspec/util/one_spec.rb +0 -23
  107. data/spec/rubocop/rspec/wording_spec.rb +0 -52
  108. data/spec/shared/autocorrect_behavior.rb +0 -9
  109. data/spec/shared/detects_style_behavior.rb +0 -9
  110. data/spec/shared/smoke_test_examples.rb +0 -27
  111. data/spec/smoke_tests/empty_spec.rb +0 -0
  112. data/spec/smoke_tests/factory_bot_spec.rb +0 -11
  113. data/spec/smoke_tests/no_tests_spec.rb +0 -4
  114. data/spec/smoke_tests/weird_rspec_spec.rb +0 -233
  115. data/spec/spec_helper.rb +0 -42
  116. data/spec/support/expect_offense.rb +0 -19
@@ -1,95 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe RuboCop::Cop::RSpec::Cop do
4
- subject(:cop) { fake_cop.new(config) }
5
-
6
- let(:config) do
7
- rubocop_config =
8
- {
9
- 'AllCops' => {
10
- 'RSpec' => {
11
- 'Patterns' => rspec_patterns
12
- }
13
- },
14
- 'RSpec/FakeCop' => {
15
- 'Exclude' => %w[bar_spec.rb]
16
- }
17
- }
18
-
19
- RuboCop::Config.new(rubocop_config, 'fake_cop_config.yml')
20
- end
21
-
22
- let(:fake_cop) do
23
- stub_const('RuboCop::RSpec', Module.new)
24
- # rubocop:disable ClassAndModuleChildren, RSpec/LeakyConstantDeclaration
25
- class RuboCop::RSpec::FakeCop < described_class
26
- def on_send(node)
27
- add_offense(node, message: 'I flag everything')
28
- end
29
- end
30
- # rubocop:enable ClassAndModuleChildren, RSpec/LeakyConstantDeclaration
31
- RuboCop::RSpec::FakeCop
32
- end
33
-
34
- let(:rspec_patterns) { ['_spec.rb$', '(?:^|/)spec/'] }
35
-
36
- context 'when the source path ends with `_spec.rb`' do
37
- it 'registers an offense' do
38
- expect_offense(<<-RUBY, 'foo_spec.rb')
39
- foo(1)
40
- ^^^^^^ I flag everything
41
- RUBY
42
- end
43
-
44
- it 'ignores the file if it is ignored' do
45
- expect_no_offenses(<<-RUBY, 'bar_spec.rb')
46
- foo(1)
47
- RUBY
48
- end
49
- end
50
-
51
- context 'when the source path contains `/spec/`' do
52
- it 'registers an offense' do
53
- expect_offense(<<-RUBY, '/spec/support/thing.rb')
54
- foo(1)
55
- ^^^^^^ I flag everything
56
- RUBY
57
- end
58
- end
59
-
60
- context 'when the source path starts with `spec/`' do
61
- it 'registers an offense' do
62
- expect_offense(<<-RUBY, 'spec/support/thing.rb')
63
- foo(1)
64
- ^^^^^^ I flag everything
65
- RUBY
66
- end
67
- end
68
-
69
- context 'when the file is a source file without "spec" in the name' do
70
- it 'ignores the source when the path is not a spec file' do
71
- expect_no_offenses(<<-RUBY, 'foo.rb')
72
- foo(1)
73
- RUBY
74
- end
75
-
76
- it 'ignores the source when the path is not a specified pattern' do
77
- expect_no_offenses(<<-RUBY, 'foo_test.rb')
78
- foo(1)
79
- RUBY
80
- end
81
- end
82
-
83
- context 'when custom patterns are specified' do
84
- let(:rspec_patterns) do
85
- ['_test\.rb$']
86
- end
87
-
88
- it 'registers offenses when the path matches a custom specified pattern' do
89
- expect_offense(<<-RUBY, 'foo_test.rb')
90
- foo(1)
91
- ^^^^^^ I flag everything
92
- RUBY
93
- end
94
- end
95
- end
@@ -1,168 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe RuboCop::Cop::RSpec::DescribeClass do
4
- subject(:cop) { described_class.new }
5
-
6
- it 'checks first-line describe statements' do
7
- expect_offense(<<-RUBY)
8
- describe "bad describe" do
9
- ^^^^^^^^^^^^^^ The first argument to describe should be the class or module being tested.
10
- end
11
- RUBY
12
- end
13
-
14
- it 'supports RSpec.describe' do
15
- expect_no_offenses(<<-RUBY)
16
- RSpec.describe Foo do
17
- end
18
- RUBY
19
- end
20
-
21
- it 'supports ::RSpec.describe' do
22
- expect_no_offenses(<<-RUBY)
23
- ::RSpec.describe Foo do
24
- end
25
- RUBY
26
- end
27
-
28
- it 'checks describe statements after a require' do
29
- expect_offense(<<-RUBY)
30
- require 'spec_helper'
31
- describe "bad describe" do
32
- ^^^^^^^^^^^^^^ The first argument to describe should be the class or module being tested.
33
- end
34
- RUBY
35
- end
36
-
37
- it 'checks highlights the first argument of a describe' do
38
- expect_offense(<<-RUBY)
39
- describe "bad describe", "blah blah" do
40
- ^^^^^^^^^^^^^^ The first argument to describe should be the class or module being tested.
41
- end
42
- RUBY
43
- end
44
-
45
- it 'ignores nested describe statements' do
46
- expect_no_offenses(<<-RUBY)
47
- describe Some::Class do
48
- describe "bad describe" do
49
- end
50
- end
51
- RUBY
52
- end
53
-
54
- it 'ignores request specs' do
55
- expect_no_offenses(<<-RUBY)
56
- describe 'my new feature', type: :request do
57
- end
58
- RUBY
59
- end
60
-
61
- it 'ignores feature specs' do
62
- expect_no_offenses(<<-RUBY)
63
- describe 'my new feature', type: :feature do
64
- end
65
- RUBY
66
- end
67
-
68
- it 'ignores system specs' do
69
- expect_no_offenses(<<-RUBY)
70
- describe 'my new system test', type: :system do
71
- end
72
- RUBY
73
- end
74
-
75
- it 'ignores feature specs when RSpec.describe is used' do
76
- expect_no_offenses(<<-RUBY)
77
- RSpec.describe 'my new feature', type: :feature do
78
- end
79
- RUBY
80
- end
81
-
82
- it 'flags specs with non :type metadata' do
83
- expect_offense(<<-RUBY)
84
- describe 'my new feature', foo: :feature do
85
- ^^^^^^^^^^^^^^^^ The first argument to describe should be the class or module being tested.
86
- end
87
- RUBY
88
- end
89
-
90
- it 'flags normal metadata in describe' do
91
- expect_offense(<<-RUBY)
92
- describe 'my new feature', blah, type: :wow do
93
- ^^^^^^^^^^^^^^^^ The first argument to describe should be the class or module being tested.
94
- end
95
- RUBY
96
- end
97
-
98
- it 'ignores feature specs - also with complex options' do
99
- expect_no_offenses(<<-RUBY)
100
- describe 'my new feature', :test, :type => :feature, :foo => :bar do
101
- end
102
- RUBY
103
- end
104
-
105
- it 'ignores an empty describe' do
106
- expect_no_offenses(<<-RUBY)
107
- RSpec.describe do
108
- end
109
-
110
- describe do
111
- end
112
- RUBY
113
- end
114
-
115
- it 'ignores routing specs' do
116
- expect_no_offenses(<<-RUBY)
117
- describe 'my new route', type: :routing do
118
- end
119
- RUBY
120
- end
121
-
122
- it 'ignores view specs' do
123
- expect_no_offenses(<<-RUBY)
124
- describe 'widgets/index', type: :view do
125
- end
126
- RUBY
127
- end
128
-
129
- it "doesn't blow up on single-line describes" do
130
- expect_no_offenses('describe Some::Class')
131
- end
132
-
133
- it "doesn't flag top level describe in a shared example" do
134
- expect_no_offenses(<<-RUBY)
135
- shared_examples 'Common::Interface' do
136
- describe '#public_interface' do
137
- it 'conforms to interface' do
138
- # ...
139
- end
140
- end
141
- end
142
- RUBY
143
- end
144
-
145
- it "doesn't flag top level describe in a shared context" do
146
- expect_no_offenses(<<-RUBY)
147
- RSpec.shared_context 'Common::Interface' do
148
- describe '#public_interface' do
149
- it 'conforms to interface' do
150
- # ...
151
- end
152
- end
153
- end
154
- RUBY
155
- end
156
-
157
- it "doesn't flag top level describe in an unnamed shared context" do
158
- expect_no_offenses(<<-RUBY)
159
- shared_context do
160
- describe '#public_interface' do
161
- it 'conforms to interface' do
162
- # ...
163
- end
164
- end
165
- end
166
- RUBY
167
- end
168
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe RuboCop::Cop::RSpec::DescribeMethod do
4
- subject(:cop) { described_class.new }
5
-
6
- it 'ignores describes with only a class' do
7
- expect_no_offenses('describe Some::Class do; end')
8
- end
9
-
10
- it 'enforces non-method names' do
11
- expect_offense(<<-RUBY)
12
- describe Some::Class, 'nope', '.incorrect_usage' do
13
- ^^^^^^ The second argument to describe should be the method being tested. '#instance' or '.class'.
14
- end
15
- RUBY
16
- end
17
-
18
- it 'skips methods starting with a . or #' do
19
- expect_no_offenses(<<-RUBY)
20
- describe Some::Class, '.asdf' do
21
- end
22
-
23
- describe Some::Class, '#fdsa' do
24
- end
25
- RUBY
26
- end
27
-
28
- it 'skips specs not having a string second argument' do
29
- expect_no_offenses(<<-RUBY)
30
- describe Some::Class, :config do
31
- end
32
- RUBY
33
- end
34
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe RuboCop::Cop::RSpec::DescribeSymbol do
4
- subject(:cop) { described_class.new }
5
-
6
- it 'flags violations for `describe :symbol`' do
7
- expect_offense(<<-RUBY)
8
- describe(:some_method) { }
9
- ^^^^^^^^^^^^ Avoid describing symbols.
10
- RUBY
11
- end
12
-
13
- it 'flags violations for `describe :symbol` with multiple arguments' do
14
- expect_offense(<<-RUBY)
15
- describe(:some_method, "description") { }
16
- ^^^^^^^^^^^^ Avoid describing symbols.
17
- RUBY
18
- end
19
-
20
- it 'flags violations for `RSpec.describe :symbol`' do
21
- expect_offense(<<-RUBY)
22
- RSpec.describe(:some_method, "description") { }
23
- ^^^^^^^^^^^^ Avoid describing symbols.
24
- RUBY
25
- end
26
-
27
- it 'flags violations for a nested `describe`' do
28
- expect_offense(<<-RUBY)
29
- RSpec.describe Foo do
30
- describe :to_s do
31
- ^^^^^ Avoid describing symbols.
32
- end
33
- end
34
- RUBY
35
- end
36
-
37
- it 'does not flag non-Symbol arguments' do
38
- expect_no_offenses('describe("#some_method") { }')
39
- end
40
-
41
- it 'does not flag `context :symbol`' do
42
- expect_no_offenses('context(:some_method) { }')
43
- end
44
- end
@@ -1,352 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- RSpec.describe RuboCop::Cop::RSpec::DescribedClass, :config do
4
- subject(:cop) { described_class.new(config) }
5
-
6
- let(:cop_config) { {} }
7
-
8
- context 'when SkipBlocks is `true`' do
9
- let(:cop_config) { { 'SkipBlocks' => true } }
10
-
11
- it 'ignores violations within non-rspec blocks' do
12
- expect_offense(<<-RUBY)
13
- describe MyClass do
14
- controller(ApplicationController) do
15
- bar = MyClass
16
- end
17
-
18
- before do
19
- MyClass
20
- ^^^^^^^ Use `described_class` instead of `MyClass`.
21
-
22
- Foo.custom_block do
23
- MyClass
24
- end
25
- end
26
- end
27
- RUBY
28
- end
29
- end
30
-
31
- context 'when SkipBlocks is `false`' do
32
- it 'flags violations within all blocks' do
33
- expect_offense(<<-RUBY)
34
- describe MyClass do
35
- controller(ApplicationController) do
36
- bar = MyClass
37
- ^^^^^^^ Use `described_class` instead of `MyClass`.
38
- end
39
-
40
- before do
41
- MyClass
42
- ^^^^^^^ Use `described_class` instead of `MyClass`.
43
-
44
- Foo.custom_block do
45
- MyClass
46
- ^^^^^^^ Use `described_class` instead of `MyClass`.
47
- end
48
- end
49
- end
50
- RUBY
51
- end
52
- end
53
-
54
- context 'when EnforcedStyle is :described_class' do
55
- let(:cop_config) { { 'EnforcedStyle' => :described_class } }
56
-
57
- it 'flags for the use of the described class' do
58
- expect_offense(<<-RUBY)
59
- describe MyClass do
60
- include MyClass
61
- ^^^^^^^ Use `described_class` instead of `MyClass`.
62
-
63
- subject { MyClass.do_something }
64
- ^^^^^^^ Use `described_class` instead of `MyClass`.
65
-
66
- before { MyClass.do_something }
67
- ^^^^^^^ Use `described_class` instead of `MyClass`.
68
- end
69
- RUBY
70
-
71
- expect_correction(<<-RUBY)
72
- describe MyClass do
73
- include described_class
74
-
75
- subject { described_class.do_something }
76
-
77
- before { described_class.do_something }
78
- end
79
- RUBY
80
- end
81
-
82
- it 'flags with metadata' do
83
- expect_offense(<<-RUBY)
84
- describe MyClass, some: :metadata do
85
- subject { MyClass }
86
- ^^^^^^^ Use `described_class` instead of `MyClass`.
87
- end
88
- RUBY
89
- end
90
-
91
- it 'ignores described class as string' do
92
- expect_no_offenses(<<-RUBY)
93
- describe MyClass do
94
- subject { "MyClass" }
95
- end
96
- RUBY
97
- end
98
-
99
- it 'ignores describe that do not reference to a class' do
100
- expect_no_offenses(<<-RUBY)
101
- describe "MyClass" do
102
- subject { "MyClass" }
103
- end
104
- RUBY
105
- end
106
-
107
- it 'ignores class if the scope is changing' do
108
- expect_no_offenses(<<-RUBY)
109
- describe MyClass do
110
- Class.new { foo = MyClass }
111
- Module.new { bar = MyClass }
112
- Struct.new { lol = MyClass }
113
-
114
- def method
115
- include MyClass
116
- end
117
-
118
- class OtherClass
119
- include MyClass
120
- end
121
-
122
- module MyModule
123
- include MyClass
124
- end
125
- end
126
- RUBY
127
- end
128
-
129
- it 'takes class from innermost describe' do
130
- expect_offense(<<-RUBY)
131
- describe MyClass do
132
- describe MyClass::Foo do
133
- subject { MyClass::Foo }
134
- ^^^^^^^^^^^^ Use `described_class` instead of `MyClass::Foo`.
135
-
136
- let(:foo) { MyClass }
137
- end
138
- end
139
- RUBY
140
- end
141
-
142
- it 'ignores subclasses' do
143
- expect_no_offenses(<<-RUBY)
144
- describe MyClass do
145
- subject { MyClass::SubClass }
146
- end
147
- RUBY
148
- end
149
-
150
- it 'ignores non-matching namespace defined on `describe` level' do
151
- expect_no_offenses(<<-RUBY)
152
- describe MyNamespace::MyClass do
153
- subject { ::MyClass }
154
- let(:foo) { MyClass }
155
- end
156
- RUBY
157
- end
158
-
159
- it 'ignores non-matching namespace' do
160
- expect_no_offenses(<<-RUBY)
161
- module MyNamespace
162
- describe MyClass do
163
- subject { ::MyClass }
164
- end
165
- end
166
- RUBY
167
- end
168
-
169
- it 'flags the use of described class with namespace' do
170
- expect_offense(<<-RUBY)
171
- describe MyNamespace::MyClass do
172
- subject { MyNamespace::MyClass }
173
- ^^^^^^^^^^^^^^^^^^^^ Use `described_class` instead of `MyNamespace::MyClass`.
174
- end
175
- RUBY
176
- end
177
-
178
- it 'ignores non-matching namespace in usages' do
179
- expect_no_offenses(<<-RUBY)
180
- module UnrelatedNamespace
181
- describe MyClass do
182
- subject { MyNamespace::MyClass }
183
- end
184
- end
185
- RUBY
186
- end
187
-
188
- it 'ignores violations within a class scope change' do
189
- expect_no_offenses(<<-RUBY)
190
- describe MyNamespace::MyClass do
191
- before do
192
- class Foo
193
- thing = MyNamespace::MyClass.new
194
- end
195
- end
196
- end
197
- RUBY
198
- end
199
-
200
- it 'ignores violations within a hook scope change' do
201
- expect_no_offenses(<<-RUBY)
202
- describe do
203
- before do
204
- MyNamespace::MyClass.new
205
- end
206
- end
207
- RUBY
208
- end
209
-
210
- it 'flags the use of described class with module' do
211
- expect_offense(<<-RUBY)
212
- module MyNamespace
213
- describe MyClass do
214
- subject { MyNamespace::MyClass }
215
- ^^^^^^^^^^^^^^^^^^^^ Use `described_class` instead of `MyNamespace::MyClass`.
216
- end
217
- end
218
- RUBY
219
-
220
- expect_correction(<<-RUBY)
221
- module MyNamespace
222
- describe MyClass do
223
- subject { described_class }
224
- end
225
- end
226
- RUBY
227
- end
228
-
229
- it 'flags the use of described class with nested namespace' do
230
- expect_offense(<<-RUBY)
231
- module A
232
- class B::C
233
- module D
234
- describe E do
235
- subject { A::B::C::D::E }
236
- ^^^^^^^^^^^^^ Use `described_class` instead of `A::B::C::D::E`.
237
- let(:one) { B::C::D::E }
238
- ^^^^^^^^^^ Use `described_class` instead of `B::C::D::E`.
239
- let(:two) { C::D::E }
240
- ^^^^^^^ Use `described_class` instead of `C::D::E`.
241
- let(:six) { D::E }
242
- ^^^^ Use `described_class` instead of `D::E`.
243
- let(:ten) { E }
244
- ^ Use `described_class` instead of `E`.
245
- end
246
- end
247
- end
248
- end
249
- RUBY
250
- end
251
-
252
- it 'accepts an empty block' do
253
- expect_no_offenses(<<-RUBY)
254
- describe MyClass do
255
- end
256
- RUBY
257
- end
258
-
259
- it 'ignores if a local variable is part of the namespace' do
260
- expect_no_offenses(<<-RUBY)
261
- describe Broken do
262
- [Foo, Bar].each do |klass|
263
- describe klass::Baz.name do
264
- it { }
265
- end
266
- end
267
- end
268
- RUBY
269
- end
270
- end
271
-
272
- context 'when EnforcedStyle is :explicit' do
273
- let(:cop_config) { { 'EnforcedStyle' => :explicit } }
274
-
275
- it 'flags the use of the described_class' do
276
- expect_offense(<<-RUBY)
277
- describe MyClass do
278
- include described_class
279
- ^^^^^^^^^^^^^^^ Use `MyClass` instead of `described_class`.
280
-
281
- subject { described_class.do_something }
282
- ^^^^^^^^^^^^^^^ Use `MyClass` instead of `described_class`.
283
-
284
- before { described_class.do_something }
285
- ^^^^^^^^^^^^^^^ Use `MyClass` instead of `described_class`.
286
- end
287
- RUBY
288
-
289
- expect_correction(<<-RUBY)
290
- describe MyClass do
291
- include MyClass
292
-
293
- subject { MyClass.do_something }
294
-
295
- before { MyClass.do_something }
296
- end
297
- RUBY
298
- end
299
-
300
- it 'ignores described_class as string' do
301
- expect_no_offenses(<<-RUBY)
302
- describe MyClass do
303
- subject { "described_class" }
304
- end
305
- RUBY
306
- end
307
-
308
- it 'ignores describe that do not reference to a class' do
309
- expect_no_offenses(<<-RUBY)
310
- describe "MyClass" do
311
- subject { described_class }
312
- end
313
- RUBY
314
- end
315
-
316
- it 'ignores violations within a class scope change' do
317
- expect_no_offenses(<<-RUBY)
318
- describe MyNamespace::MyClass do
319
- before do
320
- class Foo
321
- thing = described_class.new
322
- end
323
- end
324
- end
325
- RUBY
326
- end
327
-
328
- it 'ignores violations within a hook scope change' do
329
- expect_no_offenses(<<-RUBY)
330
- describe do
331
- before do
332
- described_class.new
333
- end
334
- end
335
- RUBY
336
- end
337
-
338
- it 'autocorrects corresponding' do
339
- expect_offense(<<-RUBY)
340
- describe(Foo) { include described_class }
341
- ^^^^^^^^^^^^^^^ Use `Foo` instead of `described_class`.
342
- describe(Bar) { include described_class }
343
- ^^^^^^^^^^^^^^^ Use `Bar` instead of `described_class`.
344
- RUBY
345
-
346
- expect_correction(<<-RUBY)
347
- describe(Foo) { include Foo }
348
- describe(Bar) { include Bar }
349
- RUBY
350
- end
351
- end
352
- end