0

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.

TPPZ
  • 4,447
  • 10
  • 61
  • 106
  • In this example provided by convey they have already implemented what u probably want https://github.com/smartystreets/goconvey/blob/ef6db91d284a0e7badaa1f0c404c30aa7dee3aed/examples/assertion_examples_test.go#L111 Do you don't want to use that? – ttomalak Apr 25 '18 at 13:28
  • Also you recieving error because you custom function not staisfy `type assertion func` which is https://github.com/smartystreets/assertions/blob/7678a5452ebea5b7090a6b163f844c133f523da2/assert/assert.go#L119 – ttomalak Apr 25 '18 at 13:34

1 Answers1

1

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())
Michael Whatcott
  • 5,603
  • 6
  • 36
  • 50