Skip to content

Commit 743e509

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

File tree

6 files changed

+41
-4
lines changed

6 files changed

+41
-4
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:

docs/modules/ROOT/pages/cops_rspec.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ A dialect can be based on the following RSpec methods:
11531153
- let, let!
11541154
- subject, subject!
11551155
- expect, is_expected, expect_any_instance_of
1156+
- raise_error, raise_exception
11561157
11571158
By default all of the RSpec methods and aliases are allowed. By setting
11581159
a config like:

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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ class << self
7272
# @!method subject?(node)
7373
def_node_matcher :subject?, '(block (send nil? #Subjects.all ...) ...)'
7474

75+
module ErrorMatchers # :nodoc:
76+
def self.all(element)
77+
Language.config['ErrorMatchers'].include?(element.to_s)
78+
end
79+
end
80+
7581
module ExampleGroups # :nodoc:
7682
class << self
7783
def all(element)
@@ -200,14 +206,14 @@ def self.all(element)
200206
# This is used in Dialect and DescribeClass cops to detect RSpec blocks.
201207
module ALL # :nodoc:
202208
def self.all(element)
203-
[ExampleGroups, Examples, Expectations, Helpers, Hooks, Includes,
204-
Runners, SharedGroups, Subjects]
209+
[ErrorMatchers, ExampleGroups, Examples, Expectations, Helpers, Hooks,
210+
Includes, Runners, SharedGroups, Subjects]
205211
.find { |concept| concept.all(element) }
206212
end
207213
end
208214

209-
private_constant :ExampleGroups, :Examples, :Expectations, :Hooks,
210-
:Includes, :Runners, :SharedGroups, :ALL
215+
private_constant :ErrorMatchers, :ExampleGroups, :Examples, :Expectations,
216+
:Hooks, :Includes, :Runners, :SharedGroups, :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)