Freezing Time
When dealing with an HTTP API that includes time-based compontents in the request (e.g. for signed S3 requests), it can be useful on playback to freeze time to what it originally was when the cassette was recorded so that the request is always the same each time your test is run.
While VCR doesn't directly support time freezing, it does expose VCR::Cassette#originally_recorded_at
, which you can easily use with a library like timecop to freeze time.
Note: VCR::Cassette#originally_recorded_at
will return nil
when the cassette is recording for the first time, so you'll probably want to use an expression like cassette.originally_recorded_at || Time.now
so that it will work when recording or when playing back.
Previously recorded responses are replayed
Given a previously recorded cassette file "cassettes/example.yml" with:
Given a file named "freeze_time.rb" with:
When I run ruby freeze_time.rb
Then it should pass with "Response: Some Event".
Last updated