Skip to content

Commit 16f349a

Browse files
committed
Always use expect_correction or expect_no_corrections when autocorrect is supported
1 parent 6fca833 commit 16f349a

8 files changed

+168
-0
lines changed

spec/rubocop/cop/rspec/described_class_spec.rb

+70
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
end
2424
end
2525
RUBY
26+
27+
expect_correction(<<~RUBY)
28+
describe MyClass do
29+
controller(ApplicationController) do
30+
bar = MyClass
31+
end
32+
33+
before do
34+
described_class
35+
36+
Foo.custom_block do
37+
MyClass
38+
end
39+
end
40+
end
41+
RUBY
2642
end
2743
end
2844

@@ -46,6 +62,22 @@
4662
end
4763
end
4864
RUBY
65+
66+
expect_correction(<<~RUBY)
67+
describe MyClass do
68+
controller(ApplicationController) do
69+
bar = described_class
70+
end
71+
72+
before do
73+
described_class
74+
75+
Foo.custom_block do
76+
described_class
77+
end
78+
end
79+
end
80+
RUBY
4981
end
5082
end
5183

@@ -95,6 +127,12 @@ module Foo
95127
^^^^^^^ Use `described_class` instead of `MyClass`.
96128
end
97129
RUBY
130+
131+
expect_correction(<<~RUBY)
132+
describe MyClass, some: :metadata do
133+
subject { described_class }
134+
end
135+
RUBY
98136
end
99137

100138
it 'ignores described class as string' do
@@ -146,6 +184,16 @@ module MyModule
146184
end
147185
end
148186
RUBY
187+
188+
expect_correction(<<~RUBY)
189+
describe MyClass do
190+
describe MyClass::Foo do
191+
subject { described_class }
192+
193+
let(:foo) { MyClass }
194+
end
195+
end
196+
RUBY
149197
end
150198

151199
it 'ignores non-matching namespace defined on `describe` level' do
@@ -174,6 +222,12 @@ module MyNamespace
174222
^^^^^^^^^^^^^^^^^^^^ Use `described_class` instead of `MyNamespace::MyClass`.
175223
end
176224
RUBY
225+
226+
expect_correction(<<~RUBY)
227+
describe MyNamespace::MyClass do
228+
subject { described_class }
229+
end
230+
RUBY
177231
end
178232

179233
it 'ignores non-matching namespace in usages' do
@@ -248,6 +302,22 @@ module D
248302
end
249303
end
250304
RUBY
305+
306+
expect_correction(<<~RUBY)
307+
module A
308+
class B::C
309+
module D
310+
describe E do
311+
subject { described_class }
312+
let(:one) { described_class }
313+
let(:two) { described_class }
314+
let(:six) { described_class }
315+
let(:ten) { described_class }
316+
end
317+
end
318+
end
319+
end
320+
RUBY
251321
end
252322

253323
it 'accepts an empty block' do

spec/rubocop/cop/rspec/empty_example_group_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767
end
6868
end
6969
RUBY
70+
71+
expect_correction("\n\n")
7072
end
7173

7274
it 'ignores example group with examples defined in `if` branches' do
@@ -123,6 +125,8 @@
123125
end
124126
end
125127
RUBY
128+
129+
expect_correction("\n\n")
126130
end
127131

128132
it 'ignores example group with examples defined in `case` branches' do
@@ -186,6 +190,8 @@
186190
end
187191
end
188192
RUBY
193+
194+
expect_correction("\n\n")
189195
end
190196

191197
it 'ignores example group with examples defined in iterator' do
@@ -222,6 +228,8 @@
222228
more_surroundings
223229
end
224230
RUBY
231+
232+
expect_correction('')
225233
end
226234

227235
it 'ignores example group with examples defined in `if` branch ' \
@@ -407,6 +415,8 @@
407415
end
408416
end
409417
RUBY
418+
419+
expect_correction('')
410420
end
411421

412422
context 'when a custom include method is specified' do

spec/rubocop/cop/rspec/empty_line_after_example_spec.rb

+21
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@
9999
it { }
100100
end
101101
RUBY
102+
103+
expect_correction(<<~RUBY)
104+
RSpec.context 'foo' do
105+
it { }
106+
it { }
107+
108+
it 'does this' do
109+
end
110+
111+
it { }
112+
it { }
113+
end
114+
RUBY
102115
end
103116

104117
it 'does not register an offense for a comment followed by an empty line' do
@@ -258,6 +271,14 @@
258271
it { two }
259272
end
260273
RUBY
274+
275+
expect_correction(<<~RUBY)
276+
RSpec.describe Foo do
277+
it { one }
278+
279+
it { two }
280+
end
281+
RUBY
261282
end
262283
end
263284
end

spec/rubocop/cop/rspec/example_wording_spec.rb

+14
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@
329329
DESC
330330
end
331331
RUBY
332+
333+
expect_no_corrections
332334
end
333335

334336
it 'flags an unclear description' do
@@ -337,6 +339,8 @@
337339
^^^^^ Your example description is insufficient.
338340
end
339341
RUBY
342+
343+
expect_no_corrections
340344
end
341345

342346
it 'flags an unclear description despite extra spaces' do
@@ -345,6 +349,8 @@
345349
^^^^^^^^^^^ Your example description is insufficient.
346350
end
347351
RUBY
352+
353+
expect_no_corrections
348354
end
349355

350356
it 'flags an unclear description despite uppercase and lowercase strings' do
@@ -353,6 +359,8 @@
353359
^^^^^^ Your example description is insufficient.
354360
end
355361
RUBY
362+
363+
expect_no_corrections
356364
end
357365

358366
context 'when `DisallowedExamples: Workz`' do
@@ -373,6 +381,8 @@
373381
" " do
374382
end
375383
RUBY
384+
385+
expect_no_corrections
376386
end
377387

378388
it 'flags an unclear description' do
@@ -381,6 +391,8 @@
381391
^^^^^ Your example description is insufficient.
382392
end
383393
RUBY
394+
395+
expect_no_corrections
384396
end
385397

386398
it 'flags an unclear description despite uppercase and lowercase strings' do
@@ -389,6 +401,8 @@
389401
^^^^^^ Your example description is insufficient.
390402
end
391403
RUBY
404+
405+
expect_no_corrections
392406
end
393407
end
394408
end

spec/rubocop/cop/rspec/expect_change_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
^^^^^^^^^^^^^^^^^^^^^ Prefer `change(User, :count)`.
4747
end
4848
RUBY
49+
50+
expect_correction(<<~RUBY)
51+
it do
52+
expect(run).to change(User, :count).by(1)
53+
end
54+
RUBY
4955
end
5056

5157
it 'ignores the usage that adheres to the enforced style' do
@@ -256,6 +262,12 @@
256262
^^^^^^^^^^^^^^^^^^^^ Prefer `change { User.count }`.
257263
end
258264
RUBY
265+
266+
expect_correction(<<~RUBY)
267+
it do
268+
expect(run).to change { User.count }.by(1)
269+
end
270+
RUBY
259271
end
260272
end
261273
end

spec/rubocop/cop/rspec/predicate_matcher_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@
257257
expect(foo).to have_something
258258
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `has_something?` over `have_something` matcher.
259259
RUBY
260+
261+
expect_correction(<<~RUBY)
262+
expect(foo.empty?).to #{matcher_true}
263+
expect(foo.empty?).to #{matcher_true}, 'fail'
264+
expect(foo.empty?).to #{matcher_false}
265+
expect(foo.has_something?).to #{matcher_true}
266+
RUBY
260267
end
261268

262269
it 'registers an offense for a predicate mather with argument' do
@@ -266,6 +273,11 @@
266273
expect(foo).to have_key(1)
267274
^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `has_key?` over `have_key` matcher.
268275
RUBY
276+
277+
expect_correction(<<~RUBY)
278+
expect(foo.something?(1, 2)).to #{matcher_true}
279+
expect(foo.has_key?(1)).to #{matcher_true}
280+
RUBY
269281
end
270282

271283
it 'registers an offense for a predicate matcher with a block' do
@@ -279,6 +291,13 @@
279291
expect(foo).to be_something(x) { |y| y.present? }
280292
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `something?` over `be_something` matcher.
281293
RUBY
294+
295+
expect_correction(<<~RUBY)
296+
expect(foo.all?(&:present?)).to #{matcher_true}
297+
expect(foo.all? { |x| x.present? }).to #{matcher_true}
298+
expect(foo.all? { present }).to #{matcher_true}
299+
expect(foo.something?(x) { |y| y.present? }).to #{matcher_true}
300+
RUBY
282301
end
283302

284303
it 'accepts built in matchers' do

spec/rubocop/cop/rspec/return_from_stub_spec.rb

+12
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ def stub_foo
218218
^^^^^^^^^^ Use block for static values.
219219
end
220220
RUBY
221+
222+
expect_correction(<<~RUBY)
223+
it do
224+
allow(Foo).to receive(:bar) { 42 }
225+
end
226+
RUBY
221227
end
222228

223229
it 'registers an offense for static values returned from chained method' do
@@ -227,6 +233,12 @@ def stub_foo
227233
^^^^^^^^^^ Use block for static values.
228234
end
229235
RUBY
236+
237+
expect_correction(<<~RUBY)
238+
it do
239+
allow(Foo).to receive(:bar).with(1) { 42 }
240+
end
241+
RUBY
230242
end
231243

232244
it 'registers, but does not try to autocorrect, heredocs' do

spec/rubocop/cop/rspec/sort_metadata_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@
269269
end
270270
end
271271
RUBY
272+
273+
expect_correction(<<~RUBY)
274+
RSpec.describan "Algo", :a, :b do
275+
contexto_compartido 'una situación complicada', baz: true, foo: 'bar' do
276+
end
277+
278+
ejemplo "hablando español", baz: true, foo: 'bar' do
279+
end
280+
end
281+
RUBY
272282
end
273283
end
274284
end

0 commit comments

Comments
 (0)