class RuboCop::Cop::RSpec::IteratedExpectation
end
expect([user1, user2, user3]).to all(be_valid)
it ‘validates users’ do
# good
end
[user1, user2, user3].each { |user| expect(user).to be_valid }
it ‘validates users’ do
# bad
@example
Check that ‘all` matcher is used instead of iterating over an array.
def on_block(node)
def on_block(node) each?(node) do |arg, body| if single_expectation?(body, arg) || only_expectations?(body, arg) add_offense(node.send_node) end end end
def on_numblock(node)
def on_numblock(node) each_numblock?(node) do |body| if single_expectation?(body, :_1) || only_expectations?(body, :_1) add_offense(node.send_node) end end end
def only_expectations?(body, arg)
def only_expectations?(body, arg) return false unless body.each_child_node.any? body.each_child_node.all? { |child| expectation?(child, arg) } end
def single_expectation?(body, arg)
def single_expectation?(body, arg) expectation?(body, arg) end