Class: RuboCop::Cop::RSpec::MultipleExpectations
- Includes:
- ConfigurableMax
- 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) { node.true_type? }
Instance Method Summary collapse
- #aggregate_failures?(node) ⇒ Object
- #aggregate_failures_block?(node) ⇒ Object
- #expect?(node) ⇒ Object
- #on_block(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
#aggregate_failures?(node) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/rubocop/cop/rspec/multiple_expectations.rb', line 57 def_node_matcher :aggregate_failures?, <<-PATTERN (block { (send _ _ <(sym :aggregate_failures) ...>) (send _ _ ... (hash <(pair (sym :aggregate_failures) %1) ...>)) } ...) PATTERN |
#aggregate_failures_block?(node) ⇒ Object
67 68 69 |
# File 'lib/rubocop/cop/rspec/multiple_expectations.rb', line 67 def_node_matcher :aggregate_failures_block?, <<-PATTERN (block (send nil? :aggregate_failures ...) ...) PATTERN |
#expect?(node) ⇒ Object
65 |
# File 'lib/rubocop/cop/rspec/multiple_expectations.rb', line 65 def_node_matcher :expect?, send_pattern('#Expectations.all') |
#on_block(node) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rubocop/cop/rspec/multiple_expectations.rb', line 71 def on_block(node) 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 |