gotddgoconvey

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

I basically want to assert a time.Time filed in a struct is representing a date within the last 24 hours.

// 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:

$ 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.

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.


Solution

  • 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 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())