Skip to content

False positive for RSpec/DescribedClass #2123

@seedot1234

Description

@seedot1234

The cop RSpec/DescribedClass incorrectly identifies namespaces/scopes as the described class, when that namespace/scope is a suffix of the described class.

Consider the following example:

module Xx::Yy
  RSpec.describe SomeClass do
    let(:not_the_described_class) {Yy::SomeClass.new}
  end
end

This will trigger the RSpec/DescribedClass: Use described_class instead of Yy::SomeClass. While Yy::SomeClass is a different class (and therefore creates a different object) than Xx::Yy::SomeClass.


Expected behavior

I would expect the cop not to be raised in this situation, since Yy:SomeClass is not the same as Xx::Yx::SomeClass

Actual behavior

The cop was raised: RSpec/DescribedClass: Use described_class instead of Yy::SomeClass. let(:not_the_described_class) {Yy::SomeClass.new}

Steps to reproduce the problem

Run rubocop on this code:

module Xx::Yy
  RSpec.describe SomeClass do
    let(:not_the_described_class) {Yy::SomeClass.new}
  end
end

A workaround that prevents the cop from being raised is to use ::Yy::Someclass instead of Yy::Someclass.

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

1.81.1 (using Parser 3.3.9.0, rubocop-ast 1.47.1, analyzing as Ruby 3.1, running on ruby 3.3.6) [x86_64-linux]
  - rubocop-faker 1.1.0
  - rubocop-performance 1.21.1
  - rubocop-rails 2.25.1
  - rubocop-rake 0.6.0
  - rubocop-rspec 3.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions