rubocop-rspec 1.32.0 → 1.33.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +11 -0
  3. data/Gemfile +2 -0
  4. data/Rakefile +2 -0
  5. data/config/default.yml +6 -0
  6. data/lib/rubocop-rspec.rb +3 -0
  7. data/lib/rubocop/cop/rspec/align_left_let_brace.rb +1 -1
  8. data/lib/rubocop/cop/rspec/align_right_let_brace.rb +1 -1
  9. data/lib/rubocop/cop/rspec/any_instance.rb +3 -1
  10. data/lib/rubocop/cop/rspec/around_block.rb +4 -2
  11. data/lib/rubocop/cop/rspec/be.rb +4 -2
  12. data/lib/rubocop/cop/rspec/be_eql.rb +3 -1
  13. data/lib/rubocop/cop/rspec/before_after_all.rb +1 -1
  14. data/lib/rubocop/cop/rspec/capybara/current_path_expectation.rb +5 -3
  15. data/lib/rubocop/cop/rspec/capybara/feature_methods.rb +3 -3
  16. data/lib/rubocop/cop/rspec/context_wording.rb +2 -2
  17. data/lib/rubocop/cop/rspec/describe_class.rb +4 -4
  18. data/lib/rubocop/cop/rspec/describe_method.rb +2 -2
  19. data/lib/rubocop/cop/rspec/describe_symbol.rb +2 -2
  20. data/lib/rubocop/cop/rspec/described_class.rb +3 -3
  21. data/lib/rubocop/cop/rspec/dialect.rb +76 -0
  22. data/lib/rubocop/cop/rspec/empty_example_group.rb +1 -1
  23. data/lib/rubocop/cop/rspec/empty_line_after_example_group.rb +1 -1
  24. data/lib/rubocop/cop/rspec/empty_line_after_final_let.rb +1 -1
  25. data/lib/rubocop/cop/rspec/empty_line_after_hook.rb +1 -1
  26. data/lib/rubocop/cop/rspec/empty_line_after_subject.rb +1 -1
  27. data/lib/rubocop/cop/rspec/example_length.rb +1 -1
  28. data/lib/rubocop/cop/rspec/example_without_description.rb +2 -2
  29. data/lib/rubocop/cop/rspec/example_wording.rb +2 -2
  30. data/lib/rubocop/cop/rspec/expect_actual.rb +1 -1
  31. data/lib/rubocop/cop/rspec/expect_change.rb +2 -2
  32. data/lib/rubocop/cop/rspec/expect_in_hook.rb +1 -1
  33. data/lib/rubocop/cop/rspec/expect_output.rb +1 -1
  34. data/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +21 -40
  35. data/lib/rubocop/cop/rspec/factory_bot/create_list.rb +2 -2
  36. data/lib/rubocop/cop/rspec/file_path.rb +2 -2
  37. data/lib/rubocop/cop/rspec/focus.rb +3 -3
  38. data/lib/rubocop/cop/rspec/hook_argument.rb +4 -6
  39. data/lib/rubocop/cop/rspec/hooks_before_examples.rb +2 -5
  40. data/lib/rubocop/cop/rspec/implicit_expect.rb +2 -2
  41. data/lib/rubocop/cop/rspec/implicit_subject.rb +1 -1
  42. data/lib/rubocop/cop/rspec/instance_spy.rb +1 -1
  43. data/lib/rubocop/cop/rspec/instance_variable.rb +1 -1
  44. data/lib/rubocop/cop/rspec/invalid_predicate_matcher.rb +4 -2
  45. data/lib/rubocop/cop/rspec/it_behaves_like.rb +1 -1
  46. data/lib/rubocop/cop/rspec/iterated_expectation.rb +3 -1
  47. data/lib/rubocop/cop/rspec/leading_subject.rb +1 -2
  48. data/lib/rubocop/cop/rspec/let_before_examples.rb +2 -5
  49. data/lib/rubocop/cop/rspec/let_setup.rb +1 -1
  50. data/lib/rubocop/cop/rspec/message_chain.rb +3 -1
  51. data/lib/rubocop/cop/rspec/message_expectation.rb +1 -1
  52. data/lib/rubocop/cop/rspec/message_spies.rb +3 -4
  53. data/lib/rubocop/cop/rspec/missing_example_group_argument.rb +1 -1
  54. data/lib/rubocop/cop/rspec/multiple_describes.rb +1 -1
  55. data/lib/rubocop/cop/rspec/multiple_expectations.rb +1 -1
  56. data/lib/rubocop/cop/rspec/multiple_subjects.rb +6 -2
  57. data/lib/rubocop/cop/rspec/named_subject.rb +1 -1
  58. data/lib/rubocop/cop/rspec/nested_groups.rb +3 -4
  59. data/lib/rubocop/cop/rspec/not_to_not.rb +3 -1
  60. data/lib/rubocop/cop/rspec/overwriting_setup.rb +1 -1
  61. data/lib/rubocop/cop/rspec/pending.rb +3 -3
  62. data/lib/rubocop/cop/rspec/predicate_matcher.rb +17 -14
  63. data/lib/rubocop/cop/rspec/rails/http_status.rb +2 -2
  64. data/lib/rubocop/cop/rspec/receive_counts.rb +5 -6
  65. data/lib/rubocop/cop/rspec/receive_never.rb +4 -7
  66. data/lib/rubocop/cop/rspec/repeated_description.rb +3 -1
  67. data/lib/rubocop/cop/rspec/repeated_example.rb +3 -1
  68. data/lib/rubocop/cop/rspec/return_from_stub.rb +2 -2
  69. data/lib/rubocop/cop/rspec/scattered_let.rb +1 -1
  70. data/lib/rubocop/cop/rspec/scattered_setup.rb +1 -1
  71. data/lib/rubocop/cop/rspec/shared_context.rb +2 -2
  72. data/lib/rubocop/cop/rspec/shared_examples.rb +7 -7
  73. data/lib/rubocop/cop/rspec/single_argument_message_chain.rb +3 -1
  74. data/lib/rubocop/cop/rspec/subject_stub.rb +1 -1
  75. data/lib/rubocop/cop/rspec/unspecified_exception.rb +5 -3
  76. data/lib/rubocop/cop/rspec/verified_doubles.rb +2 -2
  77. data/lib/rubocop/cop/rspec/void_expect.rb +1 -1
  78. data/lib/rubocop/cop/rspec/yield.rb +2 -4
  79. data/lib/rubocop/cop/rspec_cops.rb +3 -0
  80. data/lib/rubocop/rspec.rb +2 -0
  81. data/lib/rubocop/rspec/blank_line_separation.rb +3 -1
  82. data/lib/rubocop/rspec/config_formatter.rb +3 -1
  83. data/lib/rubocop/rspec/description_extractor.rb +3 -1
  84. data/lib/rubocop/rspec/factory_bot.rb +58 -0
  85. data/lib/rubocop/rspec/final_end_location.rb +2 -0
  86. data/lib/rubocop/rspec/inject.rb +2 -0
  87. data/lib/rubocop/rspec/language.rb +9 -2
  88. data/lib/rubocop/rspec/top_level_describe.rb +2 -0
  89. data/lib/rubocop/rspec/version.rb +1 -1
  90. data/rubocop-rspec.gemspec +3 -1
  91. data/spec/project/changelog_spec.rb +2 -0
  92. data/spec/project/default_config_spec.rb +2 -0
  93. data/spec/project/project_requires_spec.rb +2 -0
  94. data/spec/rubocop/cop/rspec/align_left_let_brace_spec.rb +22 -37
  95. data/spec/rubocop/cop/rspec/align_right_let_brace_spec.rb +21 -36
  96. data/spec/rubocop/cop/rspec/any_instance_spec.rb +2 -0
  97. data/spec/rubocop/cop/rspec/around_block_spec.rb +2 -0
  98. data/spec/rubocop/cop/rspec/be_eql_spec.rb +25 -4
  99. data/spec/rubocop/cop/rspec/be_spec.rb +2 -0
  100. data/spec/rubocop/cop/rspec/before_after_all_spec.rb +2 -0
  101. data/spec/rubocop/cop/rspec/capybara/current_path_expectation_spec.rb +2 -0
  102. data/spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb +2 -0
  103. data/spec/rubocop/cop/rspec/context_wording_spec.rb +32 -0
  104. data/spec/rubocop/cop/rspec/describe_class_spec.rb +2 -0
  105. data/spec/rubocop/cop/rspec/describe_method_spec.rb +2 -0
  106. data/spec/rubocop/cop/rspec/describe_symbol_spec.rb +2 -0
  107. data/spec/rubocop/cop/rspec/described_class_spec.rb +4 -1
  108. data/spec/rubocop/cop/rspec/dialect_spec.rb +78 -0
  109. data/spec/rubocop/cop/rspec/empty_line_after_example_group_spec.rb +40 -21
  110. data/spec/rubocop/cop/rspec/empty_line_after_final_let_spec.rb +77 -69
  111. data/spec/rubocop/cop/rspec/empty_line_after_hook_spec.rb +24 -19
  112. data/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb +16 -19
  113. data/spec/rubocop/cop/rspec/example_length_spec.rb +2 -0
  114. data/spec/rubocop/cop/rspec/example_without_description_spec.rb +2 -0
  115. data/spec/rubocop/cop/rspec/example_wording_spec.rb +45 -24
  116. data/spec/rubocop/cop/rspec/expect_change_spec.rb +22 -14
  117. data/spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb +36 -0
  118. data/spec/rubocop/cop/rspec/factory_bot/create_list_spec.rb +2 -0
  119. data/spec/rubocop/cop/rspec/file_path_spec.rb +2 -0
  120. data/spec/rubocop/cop/rspec/focus_spec.rb +2 -2
  121. data/spec/rubocop/cop/rspec/hook_argument_spec.rb +2 -0
  122. data/spec/rubocop/cop/rspec/hooks_before_examples_spec.rb +47 -32
  123. data/spec/rubocop/cop/rspec/implicit_expect_spec.rb +20 -19
  124. data/spec/rubocop/cop/rspec/implicit_subject_spec.rb +56 -68
  125. data/spec/rubocop/cop/rspec/instance_spy_spec.rb +16 -15
  126. data/spec/rubocop/cop/rspec/instance_variable_spec.rb +2 -0
  127. data/spec/rubocop/cop/rspec/invalid_predicate_matcher_spec.rb +2 -0
  128. data/spec/rubocop/cop/rspec/it_behaves_like_spec.rb +10 -12
  129. data/spec/rubocop/cop/rspec/iterated_expectation_spec.rb +2 -0
  130. data/spec/rubocop/cop/rspec/leading_subject_spec.rb +31 -47
  131. data/spec/rubocop/cop/rspec/let_before_examples_spec.rb +27 -26
  132. data/spec/rubocop/cop/rspec/message_chain_spec.rb +2 -0
  133. data/spec/rubocop/cop/rspec/missing_example_group_argument_spec.rb +2 -0
  134. data/spec/rubocop/cop/rspec/multiple_describes_spec.rb +2 -0
  135. data/spec/rubocop/cop/rspec/multiple_subjects_spec.rb +24 -37
  136. data/spec/rubocop/cop/rspec/not_to_not_spec.rb +22 -32
  137. data/spec/rubocop/cop/rspec/overwriting_setup_spec.rb +2 -0
  138. data/spec/rubocop/cop/rspec/pending_spec.rb +2 -0
  139. data/spec/rubocop/cop/rspec/predicate_matcher_spec.rb +2 -0
  140. data/spec/rubocop/cop/rspec/receive_counts_spec.rb +47 -8
  141. data/spec/rubocop/cop/rspec/receive_never_spec.rb +16 -4
  142. data/spec/rubocop/cop/rspec/return_from_stub_spec.rb +95 -72
  143. data/spec/rubocop/cop/rspec/scattered_let_spec.rb +2 -0
  144. data/spec/rubocop/cop/rspec/scattered_setup_spec.rb +2 -0
  145. data/spec/rubocop/cop/rspec/shared_context_spec.rb +29 -34
  146. data/spec/rubocop/cop/rspec/shared_examples_spec.rb +16 -34
  147. data/spec/rubocop/cop/rspec/single_argument_message_chain_spec.rb +44 -48
  148. data/spec/rubocop/cop/rspec/unspecified_exception_spec.rb +2 -0
  149. data/spec/rubocop/cop/rspec/verified_doubles_spec.rb +2 -0
  150. data/spec/rubocop/cop/rspec/void_expect_spec.rb +2 -0
  151. data/spec/rubocop/cop/rspec/yield_spec.rb +22 -21
  152. data/spec/rubocop/rspec/config_formatter_spec.rb +2 -0
  153. data/spec/rubocop/rspec/description_extractor_spec.rb +2 -0
  154. data/spec/rubocop/rspec/language/selector_set_spec.rb +2 -0
  155. data/spec/rubocop/rspec/util/one_spec.rb +2 -0
  156. data/spec/rubocop/rspec/wording_spec.rb +2 -0
  157. data/spec/shared/autocorrect_behavior.rb +2 -0
  158. data/spec/shared/detects_style_behavior.rb +2 -0
  159. data/spec/shared/smoke_test_examples.rb +2 -0
  160. data/spec/spec_helper.rb +2 -0
  161. data/spec/support/expect_offense.rb +3 -1
  162. metadata +8 -4
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
2
4
  subject(:cop) { described_class.new(config) }
3
5
 
@@ -15,6 +17,12 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
15
17
  ^ Use `and_return` for static values.
16
18
  end
17
19
  RUBY
20
+
21
+ expect_correction(<<-RUBY)
22
+ it do
23
+ allow(Foo).to receive(:bar).and_return(42)
24
+ end
25
+ RUBY
18
26
  end
19
27
 
20
28
  it 'finds empty values returned from block' do
@@ -24,6 +32,12 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
24
32
  ^ Use `and_return` for static values.
25
33
  end
26
34
  RUBY
35
+
36
+ expect_correction(<<-RUBY)
37
+ it do
38
+ allow(Foo).to receive(:bar).and_return(nil)
39
+ end
40
+ RUBY
27
41
  end
28
42
 
29
43
  it 'finds array with only static values returned from block' do
@@ -33,6 +47,12 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
33
47
  ^ Use `and_return` for static values.
34
48
  end
35
49
  RUBY
50
+
51
+ expect_correction(<<-RUBY)
52
+ it do
53
+ allow(Foo).to receive(:bar).and_return([42, 43])
54
+ end
55
+ RUBY
36
56
  end
37
57
 
38
58
  it 'finds hash with only static values returned from block' do
@@ -42,6 +62,12 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
42
62
  ^ Use `and_return` for static values.
43
63
  end
44
64
  RUBY
65
+
66
+ expect_correction(<<-RUBY)
67
+ it do
68
+ allow(Foo).to receive(:bar).and_return({a: 42, b: 43})
69
+ end
70
+ RUBY
45
71
  end
46
72
 
47
73
  it 'finds static values in a block when there are chained methods' do
@@ -51,6 +77,12 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
51
77
  ^ Use `and_return` for static values.
52
78
  end
53
79
  RUBY
80
+
81
+ expect_correction(<<-RUBY)
82
+ it do
83
+ allow(Question).to receive(:meaning).with(:universe).and_return(42)
84
+ end
85
+ RUBY
54
86
  end
55
87
 
56
88
  it 'finds constants returned from block' do
@@ -60,6 +92,12 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
60
92
  ^ Use `and_return` for static values.
61
93
  end
62
94
  RUBY
95
+
96
+ expect_correction(<<-RUBY)
97
+ it do
98
+ allow(Foo).to receive(:bar).and_return(Life::MEANING)
99
+ end
100
+ RUBY
63
101
  end
64
102
 
65
103
  it 'finds nested constants returned from block' do
@@ -69,6 +107,12 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
69
107
  ^ Use `and_return` for static values.
70
108
  end
71
109
  RUBY
110
+
111
+ expect_correction(<<-RUBY)
112
+ it do
113
+ allow(Foo).to receive(:bar).and_return({Life::MEANING => 42})
114
+ end
115
+ RUBY
72
116
  end
73
117
 
74
118
  it 'ignores dynamic values returned from block' do
@@ -125,6 +169,13 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
125
169
  end
126
170
  end
127
171
  RUBY
172
+
173
+ expect_correction(<<-RUBY)
174
+ it do
175
+ allow(Foo).to receive(:bar).and_return("You called" \
176
+ "me")
177
+ end
178
+ RUBY
128
179
  end
129
180
 
130
181
  it 'ignores stubs without return value' do
@@ -142,35 +193,6 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
142
193
  end
143
194
  RUBY
144
195
  end
145
-
146
- include_examples 'autocorrect',
147
- 'allow(Foo).to receive(:bar) { 42 }',
148
- 'allow(Foo).to receive(:bar).and_return(42)'
149
-
150
- include_examples 'autocorrect',
151
- 'allow(Foo).to receive(:bar) { { foo: 42 } }',
152
- 'allow(Foo).to receive(:bar).and_return({ foo: 42 })'
153
-
154
- include_examples 'autocorrect',
155
- 'allow(Foo).to receive(:bar).with(1) { 42 }',
156
- 'allow(Foo).to receive(:bar).with(1).and_return(42)'
157
-
158
- include_examples 'autocorrect',
159
- 'allow(Foo).to receive(:bar) {}',
160
- 'allow(Foo).to receive(:bar).and_return(nil)'
161
-
162
- original = <<-RUBY
163
- allow(Foo).to receive(:bar) do
164
- 'You called ' \\
165
- 'me'
166
- end
167
- RUBY
168
- corrected = <<-RUBY
169
- allow(Foo).to receive(:bar).and_return('You called ' \\
170
- 'me')
171
- RUBY
172
-
173
- include_examples 'autocorrect', original, corrected
174
196
  end
175
197
 
176
198
  context 'with EnforcedStyle `block`' do
@@ -219,48 +241,49 @@ RSpec.describe RuboCop::Cop::RSpec::ReturnFromStub, :config do
219
241
  RUBY
220
242
  end
221
243
 
222
- include_examples 'autocorrect',
223
- 'allow(Foo).to receive(:bar).and_return(42)',
224
- 'allow(Foo).to receive(:bar) { 42 }'
225
-
226
- include_examples 'autocorrect',
227
- 'allow(Foo).to receive(:bar).with(1).and_return(foo: 42)',
228
- 'allow(Foo).to receive(:bar).with(1) { { foo: 42 } }'
229
-
230
- include_examples 'autocorrect',
231
- 'allow(Foo).to receive(:bar).and_return({ foo: 42 })',
232
- 'allow(Foo).to receive(:bar) { { foo: 42 } }'
233
-
234
- include_examples 'autocorrect',
235
- 'allow(Foo).to receive(:bar).and_return(foo: 42)',
236
- 'allow(Foo).to receive(:bar) { { foo: 42 } }'
237
-
238
- original = <<-RUBY
239
- allow(Foo).to receive(:bar).and_return(
240
- a: 42,
241
- b: 43
242
- )
243
- RUBY
244
- corrected = <<-RUBY # Not perfect, but good enough.
245
- allow(Foo).to receive(:bar) { { a: 42,
246
- b: 43 } }
247
- RUBY
248
-
249
- include_examples 'autocorrect', original, corrected
250
-
251
- include_examples 'autocorrect',
252
- 'allow(Foo).to receive(:bar).and_return(nil)',
253
- 'allow(Foo).to receive(:bar) { nil }'
254
-
255
- original = <<-RUBY
256
- allow(Foo).to receive(:bar).and_return('You called ' \\
257
- 'me')
258
- RUBY
259
- corrected = <<-RUBY
260
- allow(Foo).to receive(:bar) { 'You called ' \\
261
- 'me' }
262
- RUBY
263
-
264
- include_examples 'autocorrect', original, corrected
244
+ it 'finds hash with only static values returned from method' do
245
+ expect_offense(<<-RUBY)
246
+ allow(Foo).to receive(:bar).and_return({ foo: 42 })
247
+ ^^^^^^^^^^ Use block for static values.
248
+ allow(Foo).to receive(:bar).and_return(foo: 42)
249
+ ^^^^^^^^^^ Use block for static values.
250
+ allow(Foo).to receive(:bar).and_return(
251
+ ^^^^^^^^^^ Use block for static values.
252
+ a: 42,
253
+ b: 43
254
+ )
255
+ RUBY
256
+
257
+ expect_correction(<<-RUBY) # Not perfect, but good enough.
258
+ allow(Foo).to receive(:bar) { { foo: 42 } }
259
+ allow(Foo).to receive(:bar) { { foo: 42 } }
260
+ allow(Foo).to receive(:bar) { { a: 42,
261
+ b: 43 } }
262
+ RUBY
263
+ end
264
+
265
+ it 'finds nil returned from method' do
266
+ expect_offense(<<-RUBY)
267
+ allow(Foo).to receive(:bar).and_return(nil)
268
+ ^^^^^^^^^^ Use block for static values.
269
+ RUBY
270
+
271
+ expect_correction(<<-RUBY)
272
+ allow(Foo).to receive(:bar) { nil }
273
+ RUBY
274
+ end
275
+
276
+ it 'finds concatenated strings with no variables' do
277
+ expect_offense(<<-RUBY)
278
+ allow(Foo).to receive(:bar).and_return('You called ' \\
279
+ ^^^^^^^^^^ Use block for static values.
280
+ 'me')
281
+ RUBY
282
+
283
+ expect_correction(<<-RUBY)
284
+ allow(Foo).to receive(:bar) { 'You called ' \\
285
+ 'me' }
286
+ RUBY
287
+ end
265
288
  end
266
289
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::ScatteredLet do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::ScatteredSetup do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::SharedContext do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -17,6 +19,13 @@ RSpec.describe RuboCop::Cop::RSpec::SharedContext do
17
19
  end
18
20
  end
19
21
  RUBY
22
+
23
+ expect_correction(<<-RUBY)
24
+ shared_examples 'foo' do
25
+ it 'performs actions' do
26
+ end
27
+ end
28
+ RUBY
20
29
  end
21
30
 
22
31
  it 'does not register an offense for `shared_context` with let' do
@@ -70,6 +79,12 @@ RSpec.describe RuboCop::Cop::RSpec::SharedContext do
70
79
  let(:foo) { :bar }
71
80
  end
72
81
  RUBY
82
+
83
+ expect_correction(<<-RUBY)
84
+ shared_context 'foo' do
85
+ let(:foo) { :bar }
86
+ end
87
+ RUBY
73
88
  end
74
89
 
75
90
  it 'registers an offense for shared_examples with only subject' do
@@ -79,6 +94,12 @@ RSpec.describe RuboCop::Cop::RSpec::SharedContext do
79
94
  subject(:foo) { :bar }
80
95
  end
81
96
  RUBY
97
+
98
+ expect_correction(<<-RUBY)
99
+ shared_context 'foo' do
100
+ subject(:foo) { :bar }
101
+ end
102
+ RUBY
82
103
  end
83
104
 
84
105
  it 'registers an offense for shared_examples with only hooks' do
@@ -90,6 +111,14 @@ RSpec.describe RuboCop::Cop::RSpec::SharedContext do
90
111
  end
91
112
  end
92
113
  RUBY
114
+
115
+ expect_correction(<<-RUBY)
116
+ shared_context 'foo' do
117
+ before do
118
+ foo
119
+ end
120
+ end
121
+ RUBY
93
122
  end
94
123
 
95
124
  it 'does not register an offense for `shared_examples` with it' do
@@ -105,38 +134,4 @@ RSpec.describe RuboCop::Cop::RSpec::SharedContext do
105
134
  RUBY
106
135
  end
107
136
  end
108
-
109
- bad_shared_context = <<-RUBY
110
- shared_context 'foo' do
111
- it 'performs actions' do
112
- end
113
- end
114
- RUBY
115
-
116
- good_shared_context = <<-RUBY
117
- shared_examples 'foo' do
118
- it 'performs actions' do
119
- end
120
- end
121
- RUBY
122
-
123
- include_examples 'autocorrect',
124
- bad_shared_context,
125
- good_shared_context
126
-
127
- bad_shared_examples = <<-RUBY
128
- shared_examples 'foo' do
129
- let(:foo) { :bar }
130
- end
131
- RUBY
132
-
133
- good_shared_examples = <<-RUBY
134
- shared_context 'foo' do
135
- let(:foo) { :bar }
136
- end
137
- RUBY
138
-
139
- include_examples 'autocorrect',
140
- bad_shared_examples,
141
- good_shared_examples
142
137
  end
@@ -18,7 +18,7 @@ RSpec.describe RuboCop::Cop::RSpec::SharedExamples do
18
18
  include_examples :foo_bar_baz, 'foo', 'bar'
19
19
  ^^^^^^^^^^^^ Prefer 'foo bar baz' over `:foo_bar_baz` to titleize shared examples.
20
20
 
21
- shared_examples :foo_bar_baz do |param|
21
+ shared_examples :foo_bar_baz, 'foo', 'bar' do |param|
22
22
  ^^^^^^^^^^^^ Prefer 'foo bar baz' over `:foo_bar_baz` to titleize shared examples.
23
23
  # ...
24
24
  end
@@ -26,6 +26,21 @@ RSpec.describe RuboCop::Cop::RSpec::SharedExamples do
26
26
  RSpec.shared_examples :foo_bar_baz
27
27
  ^^^^^^^^^^^^ Prefer 'foo bar baz' over `:foo_bar_baz` to titleize shared examples.
28
28
  RUBY
29
+
30
+ expect_correction(<<-RUBY)
31
+ it_behaves_like 'foo bar baz'
32
+ it_should_behave_like 'foo bar baz'
33
+ shared_examples 'foo bar baz'
34
+ shared_examples_for 'foo bar baz'
35
+ include_examples 'foo bar baz'
36
+ include_examples 'foo bar baz', 'foo', 'bar'
37
+
38
+ shared_examples 'foo bar baz', 'foo', 'bar' do |param|
39
+ # ...
40
+ end
41
+
42
+ RSpec.shared_examples 'foo bar baz'
43
+ RUBY
29
44
  end
30
45
 
31
46
  it 'does not register an offense when using string title' do
@@ -57,37 +72,4 @@ RSpec.describe RuboCop::Cop::RSpec::SharedExamples do
57
72
  end
58
73
  RUBY
59
74
  end
60
-
61
- include_examples 'autocorrect',
62
- 'it_behaves_like :foo_bar_baz',
63
- "it_behaves_like 'foo bar baz'"
64
- include_examples 'autocorrect',
65
- 'it_should_behave_like :foo_bar_baz',
66
- "it_should_behave_like 'foo bar baz'"
67
- include_examples 'autocorrect',
68
- 'shared_examples :foo_bar_baz',
69
- "shared_examples 'foo bar baz'"
70
- include_examples 'autocorrect',
71
- 'shared_examples_for :foo_bar_baz',
72
- "shared_examples_for 'foo bar baz'"
73
- include_examples 'autocorrect',
74
- 'include_examples :foo_bar_baz',
75
- "include_examples 'foo bar baz'"
76
- include_examples 'autocorrect',
77
- "include_examples :foo_bar_baz, 'foo', 'bar'",
78
- "include_examples 'foo bar baz', 'foo', 'bar'"
79
-
80
- bad_code_with_block = <<-RUBY
81
- shared_examples :foo_bar_baz, 'foo', 'bar' do |param|
82
- # ...
83
- end
84
- RUBY
85
-
86
- good_code_with_block = <<-RUBY
87
- shared_examples 'foo bar baz', 'foo', 'bar' do |param|
88
- # ...
89
- end
90
- RUBY
91
-
92
- include_examples 'autocorrect', bad_code_with_block, good_code_with_block
93
75
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe RuboCop::Cop::RSpec::SingleArgumentMessageChain do
2
4
  subject(:cop) { described_class.new }
3
5
 
@@ -9,13 +11,13 @@ RSpec.describe RuboCop::Cop::RSpec::SingleArgumentMessageChain do
9
11
  ^^^^^^^^^^^^^^^^^^^^^ Use `receive` instead of calling `receive_message_chain` with a single argument.
10
12
  end
11
13
  RUBY
12
- end
13
14
 
14
- include_examples(
15
- 'autocorrect',
16
- 'before { allow(foo).to receive_message_chain(:one) { :two } }',
17
- 'before { allow(foo).to receive(:one) { :two } }'
18
- )
15
+ expect_correction(<<-RUBY)
16
+ before do
17
+ allow(foo).to receive(:one) { :two }
18
+ end
19
+ RUBY
20
+ end
19
21
 
20
22
  it 'accepts multi-argument calls' do
21
23
  expect_no_offenses(<<-RUBY)
@@ -32,13 +34,13 @@ RSpec.describe RuboCop::Cop::RSpec::SingleArgumentMessageChain do
32
34
  ^^^^^^^^^^^^^^^^^^^^^ Use `receive` instead of calling `receive_message_chain` with a single argument.
33
35
  end
34
36
  RUBY
35
- end
36
37
 
37
- include_examples(
38
- 'autocorrect',
39
- 'before { allow(foo).to receive_message_chain("one") { :two } }',
40
- 'before { allow(foo).to receive("one") { :two } }'
41
- )
38
+ expect_correction(<<-RUBY)
39
+ before do
40
+ allow(foo).to receive("one") { :two }
41
+ end
42
+ RUBY
43
+ end
42
44
 
43
45
  it 'accepts multi-argument string calls' do
44
46
  expect_no_offenses(<<-RUBY)
@@ -73,13 +75,13 @@ RSpec.describe RuboCop::Cop::RSpec::SingleArgumentMessageChain do
73
75
  ^^^^^^^^^^^^^^^^^^^^^ Use `receive` instead of calling `receive_message_chain` with a single argument.
74
76
  end
75
77
  RUBY
76
- end
77
78
 
78
- include_examples(
79
- 'autocorrect',
80
- 'before { allow(foo).to receive_message_chain([:one]) { :two } }',
81
- 'before { allow(foo).to receive(:one) { :two } }'
82
- )
79
+ expect_correction(<<-RUBY)
80
+ before do
81
+ allow(foo).to receive(:one) { :two }
82
+ end
83
+ RUBY
84
+ end
83
85
  end
84
86
 
85
87
  context 'with multiple-element array argument' do
@@ -98,27 +100,21 @@ RSpec.describe RuboCop::Cop::RSpec::SingleArgumentMessageChain do
98
100
  before do
99
101
  allow(foo).to receive_message_chain(bar: 42)
100
102
  ^^^^^^^^^^^^^^^^^^^^^ Use `receive` instead of calling `receive_message_chain` with a single argument.
103
+ allow(foo).to receive_message_chain("bar" => 42)
104
+ ^^^^^^^^^^^^^^^^^^^^^ Use `receive` instead of calling `receive_message_chain` with a single argument.
105
+ allow(foo).to receive_message_chain(:"\#{foo}" => 42)
106
+ ^^^^^^^^^^^^^^^^^^^^^ Use `receive` instead of calling `receive_message_chain` with a single argument.
101
107
  end
102
108
  RUBY
103
- end
104
109
 
105
- include_examples(
106
- 'autocorrect',
107
- 'before { allow(foo).to receive_message_chain(bar: 42) }',
108
- 'before { allow(foo).to receive(:bar).and_return(42) }'
109
- )
110
-
111
- include_examples(
112
- 'autocorrect',
113
- 'before { allow(foo).to receive_message_chain("bar" => 42) }',
114
- 'before { allow(foo).to receive("bar").and_return(42) }'
115
- )
116
-
117
- include_examples(
118
- 'autocorrect',
119
- 'before { allow(foo).to receive_message_chain(:"#{foo}" => 42) }',
120
- 'before { allow(foo).to receive(:"#{foo}").and_return(42) }'
121
- )
110
+ expect_correction(<<-RUBY)
111
+ before do
112
+ allow(foo).to receive(:bar).and_return(42)
113
+ allow(foo).to receive("bar").and_return(42)
114
+ allow(foo).to receive(:"\#{foo}").and_return(42)
115
+ end
116
+ RUBY
117
+ end
122
118
  end
123
119
 
124
120
  context 'with multiple keys hash argument' do
@@ -140,13 +136,13 @@ RSpec.describe RuboCop::Cop::RSpec::SingleArgumentMessageChain do
140
136
  ^^^^^^^^^^ Use `stub` instead of calling `stub_chain` with a single argument.
141
137
  end
142
138
  RUBY
143
- end
144
139
 
145
- include_examples(
146
- 'autocorrect',
147
- 'before { foo.stub_chain(:one) { :two } }',
148
- 'before { foo.stub(:one) { :two } }'
149
- )
140
+ expect_correction(<<-RUBY)
141
+ before do
142
+ foo.stub(:one) { :two }
143
+ end
144
+ RUBY
145
+ end
150
146
 
151
147
  it 'accepts multi-argument calls' do
152
148
  expect_no_offenses(<<-RUBY)
@@ -163,13 +159,13 @@ RSpec.describe RuboCop::Cop::RSpec::SingleArgumentMessageChain do
163
159
  ^^^^^^^^^^ Use `stub` instead of calling `stub_chain` with a single argument.
164
160
  end
165
161
  RUBY
166
- end
167
162
 
168
- include_examples(
169
- 'autocorrect',
170
- 'before { foo.stub_chain("one") { :two } }',
171
- 'before { foo.stub("one") { :two } }'
172
- )
163
+ expect_correction(<<-RUBY)
164
+ before do
165
+ foo.stub("one") { :two }
166
+ end
167
+ RUBY
168
+ end
173
169
 
174
170
  it 'accepts multi-argument string calls' do
175
171
  expect_no_offenses(<<-RUBY)