Class: RuboCop::Cop::RSpecGuide::MinimumBehavioralCoverage
- Inherits:
-
RuboCop::Cop::RSpec::Base
- Object
- RuboCop::Cop::RSpec::Base
- RuboCop::Cop::RSpecGuide::MinimumBehavioralCoverage
- Defined in:
- lib/rubocop/cop/rspec_guide/minimum_behavioral_coverage.rb
Overview
Checks that describe blocks test at least 2 behavioral variations.
Testing only a single scenario (happy path OR edge case) provides insufficient coverage. Tests should verify both expected behavior and edge case handling to ensure comprehensive validation.
This can be achieved in two ways:
- Use 2+ sibling context blocks (happy path + edge cases)
- Combine it-blocks (default behavior) with context-blocks (edge cases)
Direct Known Subclasses
Constant Summary collapse
- MSG =
"Describe block should test at least 2 behavioral variations: " \ "either use 2+ sibling contexts (happy path + edge cases), " \ "or combine it-blocks for default behavior with context-blocks for edge cases. " \ "Use `# rubocop:disable RSpecGuide/MinimumBehavioralCoverage` " \ "for simple cases (e.g., getters) with no edge cases."
Instance Method Summary collapse
Instance Method Details
#context_only?(node) ⇒ Object
111 112 113 |
# File 'lib/rubocop/cop/rspec_guide/minimum_behavioral_coverage.rb', line 111 def_node_matcher :context_only?, "(block (send nil? :context ...) ...)\n" |
#on_block(node) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rubocop/cop/rspec_guide/minimum_behavioral_coverage.rb', line 115 def on_block(node) # Fast pre-check: only process describe blocks (not context) return unless node.method?(:describe) return unless example_group?(node) children = collect_children(node) contexts = children.select { |child| context_only?(child) } its = children.select { |child| example?(child) } # Valid if: 2+ contexts OR (1+ it-blocks before contexts AND 1+ contexts) return if contexts.size >= 2 return if valid_it_then_context_pattern?(children, its, contexts) add_offense(node) end |