Skip to content

Commit ece3163

Browse files
committed
Use expect_correction more
In these few cases, it was easy to use the `expect_correction` helper instead of the outdated `include_examples 'autocorrect'`. There are still four spec files using `include_examples 'autocorrect'`, and I hope we can remove them soon.
1 parent f8090f9 commit ece3163

File tree

3 files changed

+85
-74
lines changed

3 files changed

+85
-74
lines changed

spec/rubocop/cop/rspec/capybara/feature_methods_spec.rb

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
^^^^^^^^^^ Use `before` instead of `background`.
99
end
1010
RUBY
11+
12+
expect_correction(<<-RUBY)
13+
describe 'some feature' do
14+
before do; end
15+
end
16+
RUBY
1117
end
1218

1319
it 'flags violations for `scenario`' do
@@ -17,6 +23,12 @@
1723
^^^^^^^^ Use `it` instead of `scenario`.
1824
end
1925
RUBY
26+
27+
expect_correction(<<-RUBY)
28+
RSpec.describe 'some feature' do
29+
it 'Foo' do; end
30+
end
31+
RUBY
2032
end
2133

2234
it 'flags violations for `xscenario`' do
@@ -26,6 +38,12 @@
2638
^^^^^^^^^ Use `xit` instead of `xscenario`.
2739
end
2840
RUBY
41+
42+
expect_correction(<<-RUBY)
43+
describe 'Foo' do
44+
RSpec.xit 'Baz' do; end
45+
end
46+
RUBY
2947
end
3048

3149
it 'flags violations for `given`' do
@@ -35,6 +53,12 @@
3553
^^^^^ Use `let` instead of `given`.
3654
end
3755
RUBY
56+
57+
expect_correction(<<-RUBY)
58+
RSpec.describe 'Foo' do
59+
let(:foo) { :foo }
60+
end
61+
RUBY
3862
end
3963

4064
it 'flags violations for `given!`' do
@@ -44,13 +68,23 @@
4468
^^^^^^ Use `let!` instead of `given!`.
4569
end
4670
RUBY
71+
72+
expect_correction(<<-RUBY)
73+
describe 'Foo' do
74+
let!(:foo) { :foo }
75+
end
76+
RUBY
4777
end
4878

4979
it 'flags violations for `feature`' do
5080
expect_offense(<<-RUBY)
5181
RSpec.feature 'Foo' do; end
5282
^^^^^^^ Use `describe` instead of `feature`.
5383
RUBY
84+
85+
expect_correction(<<-RUBY)
86+
RSpec.describe 'Foo' do; end
87+
RUBY
5488
end
5589

5690
it 'ignores variables inside examples' do
@@ -101,26 +135,4 @@
101135
RUBY
102136
end
103137
end
104-
105-
shared_examples 'autocorrect_spec' do |original, corrected|
106-
original = <<-RUBY
107-
describe Foo do
108-
#{original}
109-
end
110-
RUBY
111-
corrected = <<-RUBY
112-
describe Foo do
113-
#{corrected}
114-
end
115-
RUBY
116-
117-
include_examples 'autocorrect', original, corrected
118-
end
119-
120-
include_examples 'autocorrect_spec', 'background { }', 'before { }'
121-
include_examples 'autocorrect_spec', 'scenario { }', 'it { }'
122-
include_examples 'autocorrect_spec', 'xscenario { }', 'xit { }'
123-
include_examples 'autocorrect_spec', 'given(:foo) { }', 'let(:foo) { }'
124-
include_examples 'autocorrect_spec', 'given!(:foo) { }', 'let!(:foo) { }'
125-
include_examples 'autocorrect_spec', 'RSpec.feature { }', 'RSpec.describe { }'
126138
end

spec/rubocop/cop/rspec/let_before_examples_spec.rb

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@
7979
RUBY
8080
end
8181

82+
it 'flags `let` with a heredoc argument' do
83+
expect_offense(<<-RUBY)
84+
RSpec.describe User do
85+
include_examples('should be after let')
86+
87+
let(:foo) { (<<-SOURCE) }
88+
^^^^^^^^^^^^^^^^^^^^^^^^^ Move `let` before the examples in the group.
89+
some long text here
90+
SOURCE
91+
end
92+
RUBY
93+
94+
expect_correction(<<-RUBY)
95+
RSpec.describe User do
96+
let(:foo) { (<<-SOURCE) }
97+
some long text here
98+
SOURCE
99+
include_examples('should be after let')
100+
101+
end
102+
RUBY
103+
end
104+
82105
it 'does not flag `let` before the examples' do
83106
expect_no_offenses(<<-RUBY)
84107
RSpec.describe User do
@@ -129,26 +152,4 @@
129152
end
130153
RUBY
131154
end
132-
133-
bad_code = <<-RUBY
134-
RSpec.describe User do
135-
include_examples('should be after let')
136-
137-
let(:foo) { (<<-SOURCE) }
138-
some long text here
139-
SOURCE
140-
end
141-
RUBY
142-
143-
good_code = <<-RUBY
144-
RSpec.describe User do
145-
let(:foo) { (<<-SOURCE) }
146-
some long text here
147-
SOURCE
148-
include_examples('should be after let')
149-
150-
end
151-
RUBY
152-
153-
include_examples 'autocorrect', bad_code, good_code
154155
end

spec/rubocop/cop/rspec/rails/http_status_spec.rb

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
it { is_expected.to have_http_status 200 }
1010
^^^ Prefer `:ok` over `200` to describe HTTP status code.
1111
RUBY
12+
13+
expect_correction(<<-RUBY)
14+
it { is_expected.to have_http_status :ok }
15+
RUBY
1216
end
1317

1418
it 'does not register an offense when using symbolic value' do
@@ -23,22 +27,17 @@
2327
RUBY
2428
end
2529

26-
include_examples 'autocorrect',
27-
'it { is_expected.to have_http_status 200 }',
28-
'it { is_expected.to have_http_status :ok }'
29-
30-
include_examples 'autocorrect',
31-
'it { is_expected.to have_http_status 404 }',
32-
'it { is_expected.to have_http_status :not_found }'
33-
3430
context 'with parenthesis' do
35-
include_examples 'autocorrect',
36-
'it { is_expected.to have_http_status(200) }',
37-
'it { is_expected.to have_http_status(:ok) }'
38-
39-
include_examples 'autocorrect',
40-
'it { is_expected.to have_http_status(404) }',
41-
'it { is_expected.to have_http_status(:not_found) }'
31+
it 'registers an offense when using numeric value' do
32+
expect_offense(<<-RUBY)
33+
it { is_expected.to have_http_status(404) }
34+
^^^ Prefer `:not_found` over `404` to describe HTTP status code.
35+
RUBY
36+
37+
expect_correction(<<-RUBY)
38+
it { is_expected.to have_http_status(:not_found) }
39+
RUBY
40+
end
4241
end
4342
end
4443

@@ -50,6 +49,10 @@
5049
it { is_expected.to have_http_status :ok }
5150
^^^ Prefer `200` over `:ok` to describe HTTP status code.
5251
RUBY
52+
53+
expect_correction(<<-RUBY)
54+
it { is_expected.to have_http_status 200 }
55+
RUBY
5356
end
5457

5558
it 'does not register an offense when using numeric value' do
@@ -67,22 +70,17 @@
6770
RUBY
6871
end
6972

70-
include_examples 'autocorrect',
71-
'it { is_expected.to have_http_status :ok }',
72-
'it { is_expected.to have_http_status 200 }'
73-
74-
include_examples 'autocorrect',
75-
'it { is_expected.to have_http_status :not_found }',
76-
'it { is_expected.to have_http_status 404 }'
77-
7873
context 'with parenthesis' do
79-
include_examples 'autocorrect',
80-
'it { is_expected.to have_http_status(:ok) }',
81-
'it { is_expected.to have_http_status(200) }'
82-
83-
include_examples 'autocorrect',
84-
'it { is_expected.to have_http_status(:not_found) }',
85-
'it { is_expected.to have_http_status(404) }'
74+
it 'registers an offense when using symbolic value' do
75+
expect_offense(<<-RUBY)
76+
it { is_expected.to have_http_status(:not_found) }
77+
^^^^^^^^^^ Prefer `404` over `:not_found` to describe HTTP status code.
78+
RUBY
79+
80+
expect_correction(<<-RUBY)
81+
it { is_expected.to have_http_status(404) }
82+
RUBY
83+
end
8684
end
8785
end
8886
end

0 commit comments

Comments
 (0)