How to fake Time.now?

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

    If you have ActiveSupport included, you could use:

    travel_to Time.zone.parse('2010-07-05 08:00')
    

    http://api.rubyonrails.org/classes/ActiveSupport/Testing/TimeHelpers.html

提交回复
热议问题