Class: RuboCop::Cop::RSpec::DescribedClass
- Extended by:
- AutoCorrector
- Includes:
- ConfigurableEnforcedStyle, Namespace
- 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, SkipBlocks and OnlyStaticConstants options. OnlyStaticConstants is only relevant when EnforcedStyle is described_class.
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
rubocop:disable InternalAffairs/NumblockHandler.
- #rspec_block?(node) ⇒ Object
- #scope_changing_syntax?(node) ⇒ Object
Methods inherited from Base
inherited, #on_new_investigation
Methods included from RSpec::Language
#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?
Instance Method Details
#common_instance_exec_closure?(node) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 85 def_node_matcher :common_instance_exec_closure?, "(block\n {\n (send (const nil? {:Class :Module :Struct}) :new ...)\n (send (const nil? :Data) :define ...)\n (send _ {:class_eval :module_eval :instance_eval} ...)\n (send _ {:class_exec :module_exec :instance_exec} ...)\n }\n ...\n)\n" |
#contains_described_class?(node) ⇒ Object
110 111 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 110 def_node_search :contains_described_class?, '(send nil? :described_class)' |
#described_constant(node) ⇒ Object
105 106 107 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 105 def_node_matcher :described_constant, "(block (send _ :describe $(const ...) ...) (args) $_)\n" |
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 113 def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler # 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
98 99 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 98 def_node_matcher :rspec_block?, '(any_block (send #rspec? #ALL.all ...) ...)' |
#scope_changing_syntax?(node) ⇒ Object
102 |
# File 'lib/rubocop/cop/rspec/described_class.rb', line 102 def_node_matcher :scope_changing_syntax?, '{def class module}' |