Skip to content

Instantly share code, notes, and snippets.

@jamescmartinez
Created October 22, 2021 15:11
Show Gist options
  • Save jamescmartinez/82eaf14dc3c71a922fbb922c146754c0 to your computer and use it in GitHub Desktop.
Save jamescmartinez/82eaf14dc3c71a922fbb922c146754c0 to your computer and use it in GitHub Desktop.
RSpec time comparison matcher to 6 decimal places
# On MacOS, time has a precision of 6. On Linux (e.g. CI), time is more precise.
#
# For example:
# Linux: Fri, 22 Oct 2021 11:24:40.219256519 UTC +00:00
# MacOS: Fri, 22 Oct 2021 11:24:40.219256000 UTC +00:00
#
# This matcher compares time with a precision of 6 decimal places to prevent
# time comparison failures in CI.
#
RSpec::Matchers.define(:eq_time) do |expected|
match do |actual|
expected.iso8601(6) == actual.iso8601(6)
end
failure_message do |actual|
"expected #{actual.iso8601(6)} to equal time #{expected.iso8601(6)}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment