Class: RuboCop::Cop::RSpec::MultipleExpectations
- Defined in:
- lib/rubocop/cop/rspec/multiple_expectations.rb
Overview
Checks if examples contain too many ‘expect` calls.
This cop is configurable using the ‘Max` option and works with `–auto-gen-config`.
Constant Summary collapse
- MSG =
'Example has too many expectations [%<total>d/%<max>d].'
- ANYTHING =
->(_node) { true }
- TRUE_NODE =
lambda(&:true_type?)
Instance Method Summary collapse
- #aggregate_failures?(node) ⇒ Object
- #aggregate_failures_block?(node) ⇒ Object
- #expect?(node) ⇒ Object
-
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler.
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
#aggregate_failures?(node) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/rubocop/cop/rspec/multiple_expectations.rb', line 78 def_node_matcher :aggregate_failures?, <<~PATTERN (block { (send _ _ <(sym :aggregate_failures) ...>) (send _ _ ... (hash <(pair (sym :aggregate_failures) %1) ...>)) } ...) PATTERN |
#aggregate_failures_block?(node) ⇒ Object
89 90 91 |
# File 'lib/rubocop/cop/rspec/multiple_expectations.rb', line 89 def_node_matcher :aggregate_failures_block?, <<~PATTERN (block (send nil? :aggregate_failures ...) ...) PATTERN |
#expect?(node) ⇒ Object
86 |
# File 'lib/rubocop/cop/rspec/multiple_expectations.rb', line 86 def_node_matcher :expect?, '(send nil? #Expectations.all ...)' |
#on_block(node) ⇒ Object
rubocop:disable InternalAffairs/NumblockHandler
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rubocop/cop/rspec/multiple_expectations.rb', line 93 def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler return unless example?(node) return if example_with_aggregate_failures?(node) expectations_count = to_enum(:find_expectation, node).count return if expectations_count <= max_expectations self.max = expectations_count flag_example(node, expectation_count: expectations_count) end |