class RuboCop::Cop::RSpec::DescribedClass

Checks that tests use ‘described_class`.

If the first argument of describe is a class, the class is exposed to each example via described_class.

This cop can be configured using the ‘EnforcedStyle`, `SkipBlocks` and `OnlyStaticConstants` options. `OnlyStaticConstants` is only relevant when `EnforcedStyle` is `described_class`.

@example ‘EnforcedStyle: described_class` (default)

# bad
describe MyClass do
  subject { MyClass.do_something }
end

# good
describe MyClass do
  subject { described_class.do_something }
end

@example ‘OnlyStaticConstants: true` (default)

# good
describe MyClass do
  subject { MyClass::CONSTANT }
end

@example ‘OnlyStaticConstants: false`

# bad
describe MyClass do
  subject { MyClass::CONSTANT }
end

@example ‘EnforcedStyle: explicit`

# bad
describe MyClass do
  subject { described_class.do_something }
end

# good
describe MyClass do
  subject { MyClass.do_something }
end

There’s a known caveat with rspec-rails’s ‘controller` helper that runs its block in a different context, and `described_class` is not available to it. `SkipBlocks` option excludes detection in all non-RSpec related blocks.

To narrow down this setting to only a specific directory, it is possible to use an overriding configuration file local to that directory.

@example ‘SkipBlocks: true`

# spec/controllers/.rubocop.yml
# RSpec/DescribedClass:
#   SkipBlocks: true

# acceptable
describe MyConcern do
  controller(ApplicationController) do
    include MyConcern
  end
end