Class: RuboCop::Cop::RSpec::DescribedClass
- Includes:
- 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 should be used instead of repeating the class.
Constant Summary collapse
- DESCRIBED_CLASS =
'described_class'.freeze
- MSG =
"Use `#{DESCRIBED_CLASS}` instead of `%s`".freeze
Constants inherited from Cop
Constants included from RSpec::Language
Instance Method Summary collapse
Methods included from RSpec::TopLevelDescribe
Methods inherited from Cop
Instance Method Details
#autocorrect(node) ⇒ Object
46 47 48 49 50 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 46 def autocorrect(node) lambda do |corrector| corrector.replace(node.loc.expression, DESCRIBED_CLASS) end end |
#on_block(node) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 37 def on_block(node) describe, described_class, body = described_constant(node) return unless top_level_describe?(describe) find_constant_usage(body, described_class) do |match| add_offense(match, :expression, format(MSG, match.const_name)) end end |