class RuboCop::Cop::RSpec::MultipleExpectations
end
end
expect(user.age).to eq(22)
expect(user.name).to eq(“John”)
it ‘builds a user’ do
describe UserCreator do
# not flagged by rubocop
# Max: 2
# RSpec/MultipleExpectations:
# .rubocop.yml
@example configuration
end
end
expect(user.age).to eq(22)
it ‘sets the users age’ do
end
expect(user.name).to eq(“John”)
it ‘sets the users name’ do
describe UserCreator do
# good
end
end
expect(user.age).to eq(22)
expect(user.name).to eq(“John”)
it ‘builds a user’ do
describe UserCreator do
# bad
@example
and works with ‘–auto-gen-config`.
This cop is configurable using the `Max` option
@see betterspecs.org/#single Single expectation test
Checks if examples contain too many `expect` calls.
def aggregated_failures_by_default?
def aggregated_failures_by_default? cop_config.fetch('AggregateFailuresByDefault', false) end
def example_with_aggregated_failures?(node)
def example_with_aggregated_failures?(node) example = node.send_node (aggregated_failures_by_default? || with_aggregated_failures?(example)) && !disabled_aggregated_failures?(example) end
def find_expectation(node, &block)
def find_expectation(node, &block) yield if expect?(node) || aggregate_failures?(node) # do not search inside of aggregate_failures block return if aggregate_failures?(node) node.each_child_node do |child| find_expectation(child, &block) end end
def flag_example(node, expectation_count:)
def flag_example(node, expectation_count:) add_offense( node.send_node, location: :expression, message: format( MSG, total: expectation_count, max: max_expectations ) ) end
def max_expectations
def max_expectations Integer(cop_config.fetch('Max', 1)) end
def on_block(node)
def on_block(node) return unless example?(node) return if example_with_aggregated_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