class RuboCop::Cop::RSpec::OverwritingSetup
let!(:other) { other }
let(:baz) { baz }
let(:foo) { bar }
subject(:test) { something }
# good
let!(:foo) { baz }
let(:foo) { bar }
let(:foo) { baz }
subject(:foo) { bar }
let(:foo) { baz }
let(:foo) { bar }
# bad
@example
Checks if there is a let/subject that overwrites an existing one.
def common_setup?(node)
def common_setup?(node) return false unless setup?(node) # Search only for setup with basic_literal arguments (e.g. :sym, :str) # or no arguments at all. node.send_node.arguments.all?(&:basic_literal?) end
def find_duplicates(node)
def find_duplicates(node) setup_expressions = Set.new node.each_child_node(:block) do |child| next unless common_setup?(child) name = if child.send_node.arguments? first_argument_name(child.send_node).to_sym else :subject end yield child, name unless setup_expressions.add?(name) end end
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler return unless example_group_with_body?(node) find_duplicates(node.body) do |duplicate, name| add_offense( duplicate, message: format(MSG, name: name) ) end end