Skip to content

Fix RSpec/DescribedClass false positive for Data objects #2086

Open
@bkuhlmann

Description

@bkuhlmann

Expected behavior

Hello. 👋 I'm running into a situation where Struct and Data behavior are not consistent when including a module. For a Struct, you don't get a RSpec/DescribedClass error but with a Data object, you do.

The reason is that you can't use include described_class as RuboCop tries to tell you because you'll get the following error:

NameError: undefined local variable or method 'described_class' for class #<Class:0x00000001048b2630>

Actual behavior

The best way to see this is when comparing/contrasting behavior between a Struct and Data object. Example:

RSpec.describe Demo do
  let :struct do
    Struct.new(:name, :label) { include Demo }
  end

  let :data do
    Data.define(:name, :label) { include Demo }
  end
end

With a Struct, you won't get a RSpec/DescribedClass error but with Data, you will.

Steps to reproduce the problem

Here's a quick example (albeit contrived) to reproduce the error:

Demo = Module.new

RSpec.describe Demo do
  describe "#initialize" do
    let :implementation do
      Data.define(:name, :label) { include Demo }
    end

    it "answers Data type" do
      expect(implementation[name: "test", label: "Test"]).to be_a(Data)
    end
  end
end

RuboCop RSpec version

1.75.5 (using Parser 3.3.8.0, Prism 1.4.0, rubocop-ast 1.44.1, analyzing as Ruby 3.4, running on ruby 3.4.3) [arm64-darwin24.4.0]
  - rubocop-capybara 2.22.1
  - rubocop-disable_syntax 0.2.0
  - rubocop-packaging 0.6.0
  - rubocop-performance 1.25.0
  - rubocop-thread_safety 0.7.2
  - rubocop-rake 0.7.1
  - rubocop-rspec 3.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions