Skip to content

Instantly share code, notes, and snippets.

@samwgoldman
Created October 17, 2012 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samwgoldman/3904149 to your computer and use it in GitHub Desktop.
Save samwgoldman/3904149 to your computer and use it in GitHub Desktop.
How to use VCR even if your tests depend on Date.today/Time.now, +/- 100 days/seconds.
VCR.configure do |config|
config.cassette_library_dir = "spec/cassettes"
config.hook_into :faraday
# If a test uses a value that depends on the current time, then the recording
# become stale as time marches on.
# This setting will replace specific "absolute time" values with time-relative
# keywords when the recording is first made.
# On subsequent replayes, the time-relative keywords are substituted with
# the appropriate value, so the value is relative tot he current time.
(-100..100).each do |offset|
date = Date.today + offset
time = Time.now + offset
config.define_cassette_placeholder("<YYYYMMDD+#{offset}>") { date.to_s }
config.define_cassette_placeholder("<HTTPDATE+#{offset}>") { time.httpdate }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment