class RuboCop::Cop::RSpec::ContainExactly
it { is_expected.to contain_exactly(content, *array) }
# good
it { is_expected.to match_array(array1 + array2) }
# good
it { is_expected.to contain_exactly(*array1, *array2) }
# bad
@example
- Prefer ‘be_empty` when using `contain_exactly` with no arguments.
- Prefer `match_array` when matching array values.
This cop checks for the following:
Checks where `contain_exactly` is used.
def autocorrect_for_populated_array(node, corrector)
def autocorrect_for_populated_array(node, corrector) arrays = node.arguments.map do |splat_node| splat_node.children.first end corrector.replace( node, "match_array(#{arrays.map(&:source).join(' + ')})" ) end
def check_populated_collection(node)
def check_populated_collection(node) return unless node.each_child_node.all?(&:splat_type?) add_offense(node) do |corrector| autocorrect_for_populated_array(node, corrector) end end
def on_send(node)
def on_send(node) return if node.arguments.empty? check_populated_collection(node) end