不知道为什么下面的自定义断言不起作用,这似乎是一个编译错误,但我使用的语法似乎符合他们 wiki 页面中解释的内容:https ://github.com/smartystreets/goconvey/wiki/Custom-断言
我基本上想断言time.Time
结构中的一个字段代表过去 24 小时内的一个日期。
// func shouldBeInTheLast24Hours(targetDate time.Time, foo time.Time) string {
func shouldBeInTheLast24Hours(targetDate time.Time) string {
if targetDate.Before(time.Now().Add(time.Duration(-24) * time.Hour)) {
return ""
} else {
return "The target date is assumed to be in the last 24 hours, go *THERE* and fix stuff"
}
}
type DateStuff struct {
VipDate time.Time
}
func TestDateStuff(t *testing.T) {
Convey("Given date stuff", t, func() {
Convey("should verify some custom assertions are working", func() {
myDateStruct := &DateStuff{VipDate: time.Now()}
// So(myDateStruct.VipDate, shouldBeInTheLast24Hours, nil) // this throws "cannot use shouldBeInTheLast24Hours (type func(time.Time, time.Time) string) as type convey.assertion in argument to convey.So"
So(myDateStruct.VipDate, shouldBeInTheLast24Hours) // this throws "cannot use shouldBeInTheLast24Hours (type func(time.Time) string) as type convey.assertion in argument to convey.So"
})
})
}
在检查我正在使用的 Go Convey 版本时,我看到:
$ cd $GOPATH/src/github.com/smartystreets/goconvey/ && git log -n 1 | grep Date
Date: Fri Aug 25 16:14:26 2017 -0600
这是在 wiki 页面上的日期(2013 年 11 月 15 日)之后,所以它不应该是更新我的$GOPATH
.
我对这种闭包语法不太熟悉,但在我看来我并没有滥用它,但是我看到了编译错误,所以我一定错过了一些陷阱。