Class: RuboCop::Cop::RSpec::MultipleExpectations

Inherits:
Base
  • Object
show all
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`.

Examples:


# bad
describe UserCreator do
  it 'builds a user' do
    expect(user.name).to eq("John")
    expect(user.age).to eq(22)
  end
end

# good
describe UserCreator do
  it 'sets the users name' do
    expect(user.name).to eq("John")
  end

  it 'sets the users age' do
    expect(user.age).to eq(22)
  end
end

configuration


# .rubocop.yml
# RSpec/MultipleExpectations:
#   Max: 2

# not flagged by rubocop
describe UserCreator do
  it 'builds a user' do
    expect(user.name).to eq("John")
    expect(user.age).to eq(22)
  end
end

See Also:

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

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

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