简体   繁体   English

GoConvey自定义断言无法按预期工作

[英]GoConvey custom assertions not working as expected

Not sure why the following custom assertion is not working, it seems a compilation error, but the syntax I am using seems compliant with what is explained in their wiki page: https://github.com/smartystreets/goconvey/wiki/Custom-Assertions 不知道为什么以下自定义断言不起作用,这似乎是编译错误,但是我使用的语法似乎符合其维基页面中所解释的内容: https : //github.com/smartystreets/goconvey/wiki/Custom-断言

I basically want to assert a time.Time filed in a struct is representing a date within the last 24 hours. 我基本上要断言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"
        })
    })
}

When checking the version of Go Convey I am using I see this: 在检查Go Convey的版本时,我使用的是:

$ cd $GOPATH/src/github.com/smartystreets/goconvey/ && git log -n 1 | grep Date
Date:   Fri Aug 25 16:14:26 2017 -0600

Which is after the date on the wiki page (Nov 15, 2013) so it should not be a matter of updating the Go Convey library in my $GOPATH . 这是Wiki页上的日期之后的日期(2013年11月15日),因此更新我的$GOPATH的Go $GOPATH$GOPATH

I am not that much familiar with this closure syntax, but it does not seem to me I am misusing it, however I see that compilation error so I must be missing some gotchas. 我对闭包语法不太熟悉,但是在我看来,我并没有滥用它,但是我看到了编译错误,因此我肯定缺少一些陷阱。

Here's how I would write that custom assertion: 这是我编写自定义断言的方式:

func shouldBeInTheLast24Hours(actual interface{}, _ ...interface{}) string {
    providedDate := actual.(time.Time)
    theshold := time.Now().Add(time.Hour * -24)
    if providedDate.After(theshold) {
        return ""
    } else {
        return "The target date is assumed to be in the last 24 hours, go *THERE* and fix stuff"
    }
}

func TestDateStuff(t *testing.T) {
    Convey("Given date stuff", t, func() {
        Convey("should verify some custom assertions are working", func() {
            vipDate := time.Now()
            So(vipDate, shouldBeInTheLast24Hours)
        })
    })
}

The function signature for a custom assertion has to match the assertion func type: 自定义断言的函数签名必须匹配assertion func类型:

// assertion is an alias for a function with a signature that the convey.So()
// method can handle. Any future or custom assertions should conform to this
// method signature. The return value should be an empty string if the assertion
// passes and a well-formed failure message if not.
type assertion func(actual interface{}, expected ...interface{}) string

But, as was stated in the comments, there's already an assertion defined that can do what you need: 但是,正如评论中所述,已经定义了一个断言可以满足您的需要:

So(vipDate, ShouldHappenWithin, time.Hour*24, time.Now())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM