Class: RuboCop::Cop::RSpec::DescribedClass
- Extended by:
- AutoCorrector
- Includes:
- ConfigurableEnforcedStyle
- 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` and `SkipBlocks` options.
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.
Constant Summary collapse
- DESCRIBED_CLASS =
'described_class'
- MSG =
'Use `%<replacement>s` instead of `%<src>s`.'
Instance Method Summary collapse
- #common_instance_exec_closure?(node) ⇒ Object
- #contains_described_class?(node) ⇒ Object
- #described_constant(node) ⇒ Object
- #on_block(node) ⇒ Object
- #rspec_block?(node) ⇒ Object
- #scope_changing_syntax?(node) ⇒ Object
Methods inherited from Base
inherited, #on_new_investigation
Methods included from RSpec::Language::NodePattern
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#common_instance_exec_closure?(node) ⇒ Object
65 66 67 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 65 def_node_matcher :common_instance_exec_closure?, <<-PATTERN (block (send (const nil? {:Class :Module :Struct}) :new ...) ...) PATTERN |
#contains_described_class?(node) ⇒ Object
81 82 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 81 def_node_search :contains_described_class?, '(send nil? :described_class)' |
#described_constant(node) ⇒ Object
76 77 78 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 76 def_node_matcher :described_constant, <<-PATTERN (block (send _ :describe $(const ...) ...) (args) $_) PATTERN |
#on_block(node) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 84 def on_block(node) # In case the explicit style is used, we need to remember what's # being described. @described_class, body = described_constant(node) return unless body find_usage(body) do |match| msg = (match.const_name) add_offense(match, message: msg) do |corrector| autocorrect(corrector, match) end end end |
#rspec_block?(node) ⇒ Object
70 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 70 def_node_matcher :rspec_block?, block_pattern('#ALL.all') |
#scope_changing_syntax?(node) ⇒ Object
73 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 73 def_node_matcher :scope_changing_syntax?, '{def class module}' |