Skip to content

Commit fa5da43

Browse files
Fix: Fix false negative for RSpec/Dialect when specified Capybara-specific methods
fixed #1951 `RuboCop::Cop::RSpec::Dialect#rspec_method?` detects if node's second child is an RSpec block using `RuboCop::RSpec::Language::ALL.all`. However, `RuboCop::Cop::RSpec::Dialect#rspec_method?` returned nil because the following Capybara-specific methods were not set in config/default.yml. - given - given! - background I remove the line that checks if it’s a `rspec_method?` and add a `inside_example_group?` check. cf. #1951 (comment) Due to the above, it wouldn't filter out are calls with an explicit receiver where the receiver is not RSpec, eg `foo.describe`. so the following examples were removed. - allows calling methods named xxx in examples
1 parent e02576f commit fa5da43

File tree

3 files changed

+177
-48
lines changed

3 files changed

+177
-48
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Master (Unreleased)
44

55
- Fix false-negative and error for `RSpec/MetadataStyle` when non-literal args are used in metadata in `EnforceStyle: hash`. ([@cbliard])
6+
- Fix false negative for `RSpec/Dialect` when specified Capybara-specific methods. ([@sanfrecce-osaka])
67

78
## 3.0.4 (2024-08-05)
89

@@ -1004,6 +1005,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
10041005
[@rspeicher]: https://github.com/rspeicher
10051006
[@rst-j]: https://github.com/RST-J
10061007
[@samrjenkins]: https://github.com/samrjenkins
1008+
[@sanfrecce-osaka]: https://github.com/sanfrecce-osaka
10071009
[@schmijos]: https://github.com/schmijos
10081010
[@seanpdoyle]: https://github.com/seanpdoyle
10091011
[@sl4vr]: https://github.com/sl4vr

lib/rubocop/cop/rspec/dialect.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,12 @@ module RSpec
5858
class Dialect < Base
5959
extend AutoCorrector
6060
include MethodPreference
61+
include InsideExampleGroup
6162

6263
MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
6364

64-
# @!method rspec_method?(node)
65-
def_node_matcher :rspec_method?, '(send #rspec? #ALL.all ...)'
66-
6765
def on_send(node)
68-
return unless rspec_method?(node)
66+
return unless inside_example_group?(node)
6967
return unless preferred_methods[node.method_name]
7068

7169
msg = format(MSG, prefer: preferred_method(node.method_name),

spec/rubocop/cop/rspec/dialect_spec.rb

Lines changed: 173 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,186 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::RSpec::Dialect do
4-
let(:cop_config) do
5-
{
6-
'PreferredMethods' => {
7-
'context' => 'describe'
8-
}
9-
}
10-
end
4+
context 'with preferred methods' do
5+
context 'when `describe` is preferred to `context`' do
6+
let(:cop_config) do
7+
{
8+
'PreferredMethods' => {
9+
'context' => 'describe'
10+
}
11+
}
12+
end
1113

12-
it 'allows describe blocks' do
13-
expect_no_offenses(<<~RUBY)
14-
describe 'display name presence' do
14+
it 'allows describe blocks' do
15+
expect_no_offenses(<<~RUBY)
16+
RSpec.describe 'context' do
17+
describe 'display name presence' do
18+
end
19+
end
20+
RUBY
1521
end
16-
RUBY
17-
end
1822

19-
it 'allows calling methods named context in examples' do
20-
expect_no_offenses(<<~RUBY)
21-
it 'tests common context invocations' do
22-
expect(request.context).to be_empty?
23+
it 'registers an offense for context blocks' do
24+
expect_offense(<<~RUBY)
25+
RSpec.describe 'context' do
26+
context 'display name presence' do
27+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `describe` over `context`.
28+
end
29+
end
30+
RUBY
31+
32+
expect_correction(<<~RUBY)
33+
RSpec.describe 'context' do
34+
describe 'display name presence' do
35+
end
36+
end
37+
RUBY
2338
end
24-
RUBY
25-
end
39+
end
2640

27-
it 'registers an offense for context blocks' do
28-
expect_offense(<<~RUBY)
29-
context 'display name presence' do
30-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `describe` over `context`.
41+
context 'when `describe` is preferred to `feature`' do
42+
let(:cop_config) do
43+
{
44+
'PreferredMethods' => {
45+
'feature' => 'describe'
46+
}
47+
}
3148
end
32-
RUBY
3349

34-
expect_correction(<<~RUBY)
35-
describe 'display name presence' do
50+
it 'allows describe blocks' do
51+
expect_no_offenses(<<~RUBY)
52+
RSpec.describe 'context' do
53+
describe 'display name presence' do
54+
end
55+
end
56+
RUBY
3657
end
37-
RUBY
38-
end
3958

40-
it 'registers an offense for RSpec.context blocks' do
41-
expect_offense(<<~RUBY)
42-
RSpec.context 'context' do
43-
^^^^^^^^^^^^^^^^^^^^^^^ Prefer `describe` over `context`.
44-
it 'tests common context invocations' do
45-
expect(request.context).to be_empty?
46-
end
59+
it 'registers an offense for feature blocks' do
60+
expect_offense(<<~RUBY)
61+
RSpec.describe 'context' do
62+
feature 'display name presence' do
63+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `describe` over `feature`.
64+
end
65+
end
66+
RUBY
67+
68+
expect_correction(<<~RUBY)
69+
RSpec.describe 'context' do
70+
describe 'display name presence' do
71+
end
72+
end
73+
RUBY
4774
end
48-
RUBY
75+
end
4976

50-
expect_correction(<<~RUBY)
51-
RSpec.describe 'context' do
52-
it 'tests common context invocations' do
53-
expect(request.context).to be_empty?
54-
end
77+
context 'when `let` is preferred to `given`' do
78+
let(:cop_config) do
79+
{
80+
'PreferredMethods' => {
81+
'given' => 'let'
82+
}
83+
}
84+
end
85+
86+
it 'allows let blocks' do
87+
expect_no_offenses(<<~RUBY)
88+
RSpec.describe 'context' do
89+
let do
90+
end
91+
end
92+
RUBY
5593
end
56-
RUBY
94+
95+
it 'registers an offense for given blocks' do
96+
expect_offense(<<~RUBY)
97+
RSpec.describe 'context' do
98+
given do
99+
^^^^^ Prefer `let` over `given`.
100+
end
101+
end
102+
RUBY
103+
104+
expect_correction(<<~RUBY)
105+
RSpec.describe 'context' do
106+
let do
107+
end
108+
end
109+
RUBY
110+
end
111+
end
112+
113+
context 'when `let!` is preferred to `given!`' do
114+
let(:cop_config) do
115+
{
116+
'PreferredMethods' => {
117+
'given!' => 'let!'
118+
}
119+
}
120+
end
121+
122+
it 'allows let! blocks' do
123+
expect_no_offenses(<<~RUBY)
124+
RSpec.describe 'context' do
125+
let! do
126+
end
127+
end
128+
RUBY
129+
end
130+
131+
it 'registers an offense for given! blocks' do
132+
expect_offense(<<~RUBY)
133+
RSpec.describe 'context' do
134+
given! do
135+
^^^^^^ Prefer `let!` over `given!`.
136+
end
137+
end
138+
RUBY
139+
140+
expect_correction(<<~RUBY)
141+
RSpec.describe 'context' do
142+
let! do
143+
end
144+
end
145+
RUBY
146+
end
147+
end
148+
149+
context 'when `before` is preferred to `background`' do
150+
let(:cop_config) do
151+
{
152+
'PreferredMethods' => {
153+
'background' => 'before'
154+
}
155+
}
156+
end
157+
158+
it 'allows before blocks' do
159+
expect_no_offenses(<<~RUBY)
160+
RSpec.describe 'context' do
161+
before do
162+
end
163+
end
164+
RUBY
165+
end
166+
167+
it 'registers an offense for background blocks' do
168+
expect_offense(<<~RUBY)
169+
RSpec.describe 'context' do
170+
background do
171+
^^^^^^^^^^ Prefer `before` over `background`.
172+
end
173+
end
174+
RUBY
175+
176+
expect_correction(<<~RUBY)
177+
RSpec.describe 'context' do
178+
before do
179+
end
180+
end
181+
RUBY
182+
end
183+
end
57184
end
58185

59186
context 'without preferred methods' do
@@ -65,9 +192,11 @@
65192

66193
it 'allows all methods blocks' do
67194
expect_no_offenses(<<~RUBY)
68-
context 'is important' do
69-
specify 'for someone to work' do
70-
everyone.should have_some_leeway
195+
RSpec.describe 'context' do
196+
context 'is important' do
197+
specify 'for someone to work' do
198+
everyone.should have_some_leeway
199+
end
71200
end
72201
end
73202
RUBY

0 commit comments

Comments
 (0)