【发布时间】:2020-09-18 19:47:17
【问题描述】:
我在这里关注另一个答案:Is there an easy way to stub out time.Now() globally during test?
所以我有这个文件,我可以在其中执行以下操作:
var timeNow = time.Now
func GenerateTimestamp() int64 {
now := timeNow() // current local time
sec := now.Unix() // number of seconds since January 1, 1970 UTC
return sec // int 64
}
我们在另一个函数中使用 GenerateTimestamp()
func AddTs() {
// Some if check, use GenerateTimestamp() here
}
现在在我的测试文件中,我正在做类似的事情:
now := time.Now()
timeNow = func() int64 {
// fmt.Println("looking for this", now.Unix())
return now.Unix()
}
我收到此错误cannot use func literal (type func() int64) as type func() time.Time in assignment。我需要能够返回一个 int64 类型(我的原始函数返回),我该如何解决这个问题?
请随时将我指向文档,我是 Go 新手!!
【问题讨论】:
标签: go time stubbing go-testing