Class: RuboCop::Cop::RSpec::IteratedExpectation

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/iterated_expectation.rb

Overview

Check that ‘all` matcher is used instead of iterating over an array.

Examples:

# bad
it 'validates users' do
  [user1, user2, user3].each { |user| expect(user).to be_valid }
end

# good
it 'validates users' do
  expect([user1, user2, user3]).to all(be_valid)
end

Constant Summary collapse

MSG =
'Prefer using the `all` matcher instead ' \
'of iterating over an array.'

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#on_block(node) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/rubocop/cop/rspec/iterated_expectation.rb', line 34

def on_block(node)
  each?(node) do |arg, body|
    if single_expectation?(body, arg) || only_expectations?(body, arg)
      add_offense(node.send_node, location: :expression)
    end
  end
end