Class: RuboCop::Cop::RSpec::DescribedClass

Inherits:
RuboCop::Cop show all
Includes:
RSpec::TopLevelDescribe
Defined in:
lib/rubocop/cop/rspec/described_class.rb

Overview

If the first argument of describe is a class, the class is exposed to each example via described_class - this should be used instead of repeating the class.

Examples:

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

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

Constant Summary collapse

MESSAGE =
'Use `described_class` instead of `%s`'.freeze

Instance Method Summary collapse

Methods included from RSpec::TopLevelDescribe

#on_send

Instance Method Details

#autocorrect(node) ⇒ Object



37
38
39
40
41
# File 'lib/rubocop/cop/rspec/described_class.rb', line 37

def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.loc.expression, 'described_class')
  end
end

#on_block(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/rubocop/cop/rspec/described_class.rb', line 26

def on_block(node)
  method, _args, body = *node
  return unless top_level_describe?(method)

  _receiver, method_name, object = *method
  return unless method_name == :describe
  return unless object && object.type == :const

  inspect_children(body, object)
end