How to fake Time.now?

前端 未结 14 1144
不知归路
不知归路 2020-12-02 11:47

What\'s the best way to set Time.now for the purpose of testing time-sensitive methods in a unit test?

14条回答
  •  清歌不尽
    2020-12-02 12:41

    Do the time-warp

    time-warp is a library that does what you want. It gives you a method that takes a time and a block and anything that happens in the block uses the faked time.

    pretend_now_is(2000,"jan",1,0) do
      Time.now
    end
    

提交回复
热议问题