douqian6194 2018-04-25 11:16
浏览 51
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • duanjianhe1388 2018-07-27 19:20
    关注

    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())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题