Skip to content

Commit 278245a

Browse files
committed
Fix RSpec/DescribedClass to ignore *_eval and *_exec blocks
1 parent a1f68a0 commit 278245a

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

CHANGELOG.md

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

55
- Fix a false positive for `RSpec/LeakyLocalVariable` when variables are used only in example metadata (e.g., skip messages). ([@ydah])
66
- Fix a false positive for `RSpec/ScatteredSetup` when the hook is defined inside a class method. ([@d4rky-pl])
7+
- Fix a false positive for `RSpec/DescribedClass` inside dynamically evaluated blocks (`class_eval`, `module_eval`, `instance_eval`, `class_exec`, `module_exec`, `instance_exec`). ([@sucicfilip])
78

89
## 3.8.0 (2025-11-12)
910

@@ -1083,6 +1084,7 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features.
10831084
[@smcgivern]: https://github.com/smcgivern
10841085
[@splattael]: https://github.com/splattael
10851086
[@stephannv]: https://github.com/stephannv
1087+
[@sucicfilip]: https://github.com/sucicfilip
10861088
[@swelther]: https://github.com/swelther
10871089
[@t3h2mas]: https://github.com/t3h2mas
10881090
[@tdeo]: https://github.com/tdeo

lib/rubocop/cop/rspec/described_class.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class DescribedClass < Base # rubocop:disable Metrics/ClassLength
8787
{
8888
(send (const nil? {:Class :Module :Struct}) :new ...)
8989
(send (const nil? :Data) :define ...)
90+
(send _ {:class_eval :module_eval :instance_eval} ...)
91+
(send _ {:class_exec :module_exec :instance_exec} ...)
9092
}
9193
...
9294
)

spec/rubocop/cop/rspec/described_class_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,34 @@ module MyModule
195195
RUBY
196196
end
197197

198+
it 'ignores class inside *_eval and *_exec blocks' do
199+
expect_no_offenses(<<~RUBY)
200+
RSpec.describe Foo do
201+
before do
202+
stub_const('Dummy', Class.new).class_eval do
203+
Foo.new
204+
end
205+
206+
stub_const('Dummy', Class.new).module_eval do
207+
Foo.new
208+
end
209+
210+
stub_const('Dummy', Class.new).instance_eval do
211+
Foo.new
212+
end
213+
214+
stub_const('Dummy', Class.new).class_exec do
215+
Foo.new
216+
end
217+
218+
stub_const('Dummy', Class.new).module_exec do
219+
Foo.new
220+
end
221+
end
222+
end
223+
RUBY
224+
end
225+
198226
it 'takes class from innermost describe' do
199227
expect_offense(<<~RUBY)
200228
describe MyClass do

0 commit comments

Comments
 (0)