Class: RuboCop::Cop::RSpecGuide::ContextSetup
- Inherits:
-
RuboCop::Cop::RSpec::Base
- Object
- RuboCop::Cop::RSpec::Base
- RuboCop::Cop::RSpecGuide::ContextSetup
- Defined in:
- lib/rubocop/cop/rspec_guide/context_setup.rb
Overview
Checks that context blocks have setup (let/before) to distinguish them from the parent context.
Contexts exist to test different scenarios or states. Without explicit setup, the context doesn't actually change anything from its parent, making the context boundary meaningless and confusing.
Valid setup methods: let, let!, let_it_be, let_it_be!, before
Note: subject should be defined at describe level, not in contexts, as it describes the object under test, not context-specific state. Use RSpec/LeadingSubject cop to ensure subject is defined first.
Constant Summary collapse
- MSG =
"Context should have setup (let/let!/let_it_be/let_it_be!/before) to distinguish it from parent context"
Instance Method Summary collapse
Instance Method Details
#context_only?(node) ⇒ Object
103 104 105 |
# File 'lib/rubocop/cop/rspec_guide/context_setup.rb', line 103 def_node_matcher :context_only?, <<~PATTERN (block (send nil? :context ...) ...) PATTERN |
#on_block(node) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rubocop/cop/rspec_guide/context_setup.rb', line 107 def on_block(node) # Fast pre-check: only process context blocks return unless node.method?(:context) return unless context_only?(node) # Check if context has at least one setup node (let or before) # Note: subject is NOT counted as context setup because it describes # the object under test, not context-specific state has_setup = has_context_setup?(node) add_offense(node) unless has_setup end |