class RuboCop::Cop::RSpec::IteratedExpectation

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

@example

# 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

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/iterated_expectation.rb, line 32
def on_block(node)
  each?(node) do |arg, body|
    if single_expectation?(body, arg) || only_expectations?(body, arg)
      add_offense(node.children.first, location: :expression)
    end
  end
end

Private Instance Methods

only_expectations?(body, arg) click to toggle source
# File lib/rubocop/cop/rspec/iterated_expectation.rb, line 46
def only_expectations?(body, arg)
  body.each_child_node.all? { |child| expectation?(child, arg) }
end
single_expectation?(body, arg) click to toggle source
# File lib/rubocop/cop/rspec/iterated_expectation.rb, line 42
def single_expectation?(body, arg)
  expectation?(body, arg)
end