Skip to content

Instantly share code, notes, and snippets.

@stevepolitodesign
Last active October 25, 2021 15:00
Show Gist options
  • Save stevepolitodesign/0c728d8ba9b011b6a0020929bf3f0a91 to your computer and use it in GitHub Desktop.
Save stevepolitodesign/0c728d8ba9b011b6a0020929bf3f0a91 to your computer and use it in GitHub Desktop.
assert_in_delta example

Using assert_equal to compare dates will fail becuase they'll be off my a few miliseconds.

date = 1.month.from_now
travel_to date

@post.update(title: "Updated Title")

assert_equal date, @post.updated_at
# => No visible difference in the ActiveSupport::TimeWithZone#inspect output

Using assert_in_delta to compare dates ensures the two values are within 0.001 seconds of each other by default.

date = 1.month.from_now
travel_to date

@post.update(title: "Updated Title")

assert_in_delta date, @post.updated_at
# => Expected |2021-11-25 14:53:58 UTC - 2021-11-25 14:53:58 UTC| (0.528975) to be <= 0.001.

If you need to increase the delta just pass it in as a third argument.

date = 1.month.from_now
travel_to date

@post.update(title: "Updated Title")

assert_in_delta date, @post.updated_at, 0.75
# => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment