Skip to content

Instantly share code, notes, and snippets.

@sue445
Last active February 10, 2023 14:29
Show Gist options
  • Save sue445/8730600 to your computer and use it in GitHub Desktop.
Save sue445/8730600 to your computer and use it in GitHub Desktop.
[RSpec custom matcher] match hashes with indifferent access
# match hashes with indifferent access
#
# example)
# expect({"key1" => 1, :key2 => 2}).to equal_with_indifferent_access(key1: 1, key2: 2)
RSpec::Matchers.define :equal_with_indifferent_access do |expected|
match do |actual|
actual.with_indifferent_access == expected.with_indifferent_access
end
failure_message_for_should do |actual|
<<-EOS
expected: #{expected}
got: #{actual}
EOS
end
failure_message_for_should_not do |actual|
<<-EOS
expected: value != #{expected}
got: #{actual}
EOS
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment