Skip to content

Instantly share code, notes, and snippets.

@alexford
Created April 1, 2019 19:23
Show Gist options
  • Save alexford/6ae4d924c2856a2dc4c33f94deef0924 to your computer and use it in GitHub Desktop.
Save alexford/6ae4d924c2856a2dc4c33f94deef0924 to your computer and use it in GitHub Desktop.
Freeze system time for Ruby/RSpec

Assumptions

  • macOS 10.14
  • rbenv
  • Homebrew

Steps

Install libfaketime

https://github.com/wolfcw/libfaketime

brew install libfaketime

Set environment variables

export DYLD_FORCE_FLAT_NAMESPACE=1
export DYLD_INSERT_LIBRARIES=/usr/local/Cellar/libfaketime/0.9.7_1/lib/faketime/libfaketime.1.dylib

(The path to the library might change depending on the version)

Run ruby with rbenv which

(You cannot go through the ruby shim rbenv provides, the executable must be called directly)

With ruby -e

$ FAKETIME="@2020-12-24 20:30:00" `rbenv which ruby` -e "puts Time.now"
2020-12-24 20:30:00 -0500

With irb

$ FAKETIME="@2020-12-24 20:30:00" `rbenv which irb`
irb(main):001:0> Time.now
=> 2020-12-24 20:30:02 -0500

With Rspec

$ FAKETIME="@2020-12-24 20:30:00" `rbenv which rspec` spec/services/generate_benefits_summary_spec.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment