summaryrefslogtreecommitdiff
path: root/spec/support/matchers/match_ids.rb
blob: 1cb6b74acac3513c7fe4c14381f417a491d7aa38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
RSpec::Matchers.define :match_ids do |*expected|
  match do |actual|
    actual_ids = map_ids(actual)
    expected_ids = map_ids(expected)

    expect(actual_ids).to match_array(expected_ids)
  end

  description do
    'matches elements by ids'
  end

  failure_message do
    actual_ids = map_ids(actual)
    expected_ids = map_ids(expected)

    "expected IDs #{actual_ids} in:\n\n  #{actual.inspect}\n\nto match IDs #{expected_ids} in:\n\n  #{expected.inspect}"
  end

  def map_ids(elements)
    elements = elements.flatten if elements.respond_to?(:flatten)

    if elements.respond_to?(:map)
      elements.map(&:id)
    elsif elements.respond_to?(:id)
      [elements.id]
    else
      raise ArgumentError, "could not map elements to ids: #{elements}"
    end
  end
end