Skip to content

Commit 889d11f

Browse files
committed
Add support for error matchers to RSpec/Dialect
1 parent 47f0330 commit 889d11f

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Mark `RSpec/IncludeExamples` as `SafeAutoCorrect: false`. ([@yujideveloper])
66
- Fix a false positive for `RSpec/LeakyConstantDeclaration` when defining constants in explicit namespaces. ([@naveg])
7+
- Add support for error matchers (`raise_exception` and `raise_error`) to `RSpec/Dialect`. ([@lovro-bikic])
78

89
## 3.6.0 (2025-04-18)
910

config/default.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ RSpec:
7979
- prepend_after
8080
- after
8181
- append_after
82+
ErrorMatchers:
83+
- raise_error
84+
- raise_exception
8285
Includes:
8386
inherit_mode:
8487
merge:

lib/rubocop/cop/rspec/dialect.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module RSpec
2121
# - let, let!
2222
# - subject, subject!
2323
# - expect, is_expected, expect_any_instance_of
24+
# - raise_error, raise_exception
2425
#
2526
# By default all of the RSpec methods and aliases are allowed. By setting
2627
# a config like:

lib/rubocop/rspec/language.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ def self.all(element)
146146
end
147147
end
148148

149+
module ErrorMatchers # :nodoc:
150+
def self.all(element)
151+
Language.config['ErrorMatchers'].include?(element.to_s)
152+
end
153+
end
154+
149155
module Includes # :nodoc:
150156
class << self
151157
def all(element)
@@ -201,13 +207,13 @@ def self.all(element)
201207
module ALL # :nodoc:
202208
def self.all(element)
203209
[ExampleGroups, Examples, Expectations, Helpers, Hooks, Includes,
204-
Runners, SharedGroups, Subjects]
210+
Runners, SharedGroups, Subjects, ErrorMatchers]
205211
.find { |concept| concept.all(element) }
206212
end
207213
end
208214

209215
private_constant :ExampleGroups, :Examples, :Expectations, :Hooks,
210-
:Includes, :Runners, :SharedGroups, :ALL
216+
:Includes, :Runners, :SharedGroups, :ErrorMatchers, :ALL
211217
end
212218
end
213219
end

spec/rubocop/cop/rspec/dialect_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,29 @@
7373
RUBY
7474
end
7575
end
76+
77+
context 'with error matchers config' do
78+
let(:cop_config) do
79+
{
80+
'PreferredMethods' => {
81+
'raise_exception' => 'raise_error'
82+
}
83+
}
84+
end
85+
86+
it 'registers an offense for `raise_exception`' do
87+
expect_offense(<<~RUBY)
88+
it 'raises an error' do
89+
expect { subject }.to raise_exception(StandardError)
90+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `raise_error` over `raise_exception`.
91+
end
92+
RUBY
93+
94+
expect_correction(<<~RUBY)
95+
it 'raises an error' do
96+
expect { subject }.to raise_error(StandardError)
97+
end
98+
RUBY
99+
end
100+
end
76101
end

0 commit comments

Comments
 (0)