Skip to content

Recognize stubbing of described_class in RSpec/SubjectStub #2088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Recognize stubbing of `described_class` in `RSpec/SubjectStub`. ([@lovro-bikic])

## 3.6.0 (2025-04-18)

- Fix false positive in `RSpec/Pending`, where it would mark the default block `it` as an offense. ([@bquorning])
Expand Down
10 changes: 10 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6096,6 +6096,16 @@ describe Article do
end
end

# bad - described_class stubs are recognized as well
describe Article do
it 'indicates that the author is unknown' do
article = double(Article, description: 'by an unknown author')
allow(described_class).to receive(:new).and_return(article)

expect(Article.new.description).to include('by an unknown author')
end
end

# good
describe Article do
subject(:article) { Article.new(author: nil) }
Expand Down
12 changes: 11 additions & 1 deletion lib/rubocop/cop/rspec/subject_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ module RSpec
# end
# end
#
# # bad - described_class stubs are recognized as well
# describe Article do
# it 'indicates that the author is unknown' do
# article = double(Article, description: 'by an unknown author')
# allow(described_class).to receive(:new).and_return(article)
#
# expect(Article.new.description).to include('by an unknown author')
# end
# end
#
# # good
# describe Article do
# subject(:article) { Article.new(author: nil) }
Expand Down Expand Up @@ -141,7 +151,7 @@ def find_subject_expectations(node, subject_names = [], &block)
subject_names = [*subject_names, *@explicit_subjects[node]]
subject_names -= @subject_overrides[node] if @subject_overrides[node]

names = Set[*subject_names, :subject]
names = Set[*subject_names, :subject, :described_class]
expectation_detected = message_expectation?(node, names)
return yield(node) if expectation_detected

Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/rspec/subject_stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,17 @@
RUBY
end

it 'flags when described_class is mocked' do
expect_offense(<<~RUBY)
describe Foo do
it 'uses described_class' do
expect(described_class).to receive(:bar).and_return(baz)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub methods of the object under test.
end
end
RUBY
end

it 'flags when there are several top level example groups' do
expect_offense(<<~RUBY)
RSpec.describe Foo do
Expand Down