How to fake Time.now?

前端 未结 14 1147
不知归路
不知归路 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:35

    Don't forget that Time is merely a constant that refers to a class object. If you're willing to cause a warning, you can always do

    real_time_class = Time
    Time = FakeTimeClass
    # run test
    Time = real_time_class
    

提交回复
热议问题