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

前端 未结 7 1726
小鲜肉
小鲜肉 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 06:54

    From Google result I found relatively simple solution: Here

    The basic idea is using another function call "nowFunc" to get the time.Now(). In your main, initialize this function to return time.Now(). In your test, initialize this function to return a fixed fake time.

提交回复
热议问题