class RuboCop::Cop::RSpec::MultipleMemoizedHelpers
end
let(:bar) { [] }
let(:foo) { [] }
describe MyClass do
# bad
# Max: 1
# RSpec/MultipleMemoizedHelpers:
# rubocop.yml
@example with Max configuration
end
let(:quux) { [] }
let(:qux) { [] }
let!(:baz) { [] }
let(:bar) { [] }
let(:foo) { [] }
subject { {} }
describe MyClass do
# bad - ‘subject` counts towards memoized helpers
# AllowSubject: false
# RSpec/MultipleMemoizedHelpers:
# rubocop.yml
@example when disabling AllowSubject configuration
end
end
let(:quuz) { {} }
let(:quux) { [] }
let(:qux) { [] }
context ’when other stuff’ do
end
let!(:booger) { [] }
let(:bar) { [] }
let(:foo) { [] }
context ‘when stuff’ do
describe MyClass do
end
let(:quuz) { {} }
let(:quux) { [] }
let(:qux) { [] }
let!(:baz) { [] }
let(:bar) { [] }
describe MyClass do
# good
end
end
let(:quuz) { {} }
let(:quux) { [] }
let(:qux) { [] }
context ‘when stuff’ do
let!(:baz) { [] }
let(:bar) { [] }
let(:foo) { [] }
describe MyClass do
end
let(:quuz) { {} }
let(:quux) { [] }
let(:qux) { [] }
let!(:baz) { [] }
let(:bar) { [] }
let(:foo) { [] }
describe MyClass do
# bad
@example
`let` and not calls to ‘subject`.
which will configure the cop to only register offenses on calls to
This cop is configurable using the `Max` option and the `AllowSubject`
Checks if example groups contain too many `let` and `subject` calls.
def all_helpers(node)
def all_helpers(node) [ *helpers(node), *node.each_ancestor(:block).flat_map(&method(:helpers)) ] end
def allow_subject?
def allow_subject? cop_config['AllowSubject'] end
def helpers(node)
def helpers(node) @example_group_memoized_helpers[node] ||= variable_nodes(node).map do |variable_node| if variable_node.block_type? variable_definition?(variable_node.send_node) else # block-pass (`let(:foo, &bar)`) variable_definition?(variable_node) end end end
def max
def max cop_config['Max'] end
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler return unless spec_group?(node) count = all_helpers(node).uniq.count return if count <= max self.max = count add_offense(node, message: format(MSG, count: count, max: max)) end
def on_new_investigation
def on_new_investigation super @example_group_memoized_helpers = {} end
def variable_nodes(node)
def variable_nodes(node) example_group = RuboCop::RSpec::ExampleGroup.new(node) if allow_subject? example_group.lets else example_group.lets + example_group.subjects end end