Class: RuboCop::Cop::RSpec::DescribedClass
- Includes:
- ConfigurableEnforcedStyle, RSpec::TopLevelDescribe
- Defined in:
- lib/rubocop/cop/rspec/described_class.rb
Overview
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` option
Constant Summary collapse
- DESCRIBED_CLASS =
'described_class'- MSG =
'Use `%<replacement>s` instead of `%<src>s`.'
Constants inherited from Cop
Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE
Constants included from RSpec::Language
RSpec::Language::ALL, RSpec::Language::RSPEC
Instance Method Summary collapse
Methods included from RSpec::TopLevelDescribe
Methods inherited from Cop
Instance Method Details
#autocorrect(node) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 68 def autocorrect(node) replacement = if style == :described_class DESCRIBED_CLASS else @described_class.const_name end lambda do |corrector| corrector.replace(node.loc.expression, replacement) end end |
#on_block(node) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 51 def on_block(node) # In case the explicit style is used, we needs to remember what's # being described. Thus, we use an ivar for @described_class. describe, @described_class, body = described_constant(node) return if body.nil? return unless top_level_describe?(describe) find_usage(body) do |match| add_offense( match, location: :expression, message: (match.const_name) ) end end |