Is there an easy way to stub out time.Now() globally during test?

前端 未结 7 1684
小鲜肉
小鲜肉 2020-12-05 06:43

Part of our code is time sensitive and we need to able to reserve something and then release it in 30-60 seconds etc, which we can just do a time.Sleep(60 * time.Secon

相关标签:
7条回答
  • 2020-12-05 07:13

    We have open-sourced a library that we've been using internally for this purpose: https://github.com/cabify/timex

    It offers same API as the native time package but the implementation can be replaced in runtime from the tests.

    It is a mix of the solutions that @Flimzy proposed previously.

    0 讨论(0)
提交回复
热议问题