我正在编写一个测试,以断言一个函数在无效输入上出现恐慌,但是Ginkgo将恐慌记录为失败,而不是预期的传递结果。
func ParseUnixTimeString(unixTimeString string) time.Time {
i, err := strconv.ParseInt(unixTimeString, 10, 64)
if err != nil {
panic(fmt.Sprintf("could not parse time: %s", err.Error()))
}
return time.Unix(i, 0)
}
func TestFormat(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "Format Suite")
}
var _ = ginkgo.Describe("Format Tests", func() {
ginkgo.Describe("When formatting the date", func() {
ginkgo.It("should panic if the time can't be formatted", func() {
gomega.Expect(
tools.ParseUnixTimeString("2314321432143124223432434")).To(gomega.Panic())
})
})返回(浓缩):
•! [PANICKED] [0.002 seconds]
[It] should panic if the time can't be formatted
Test Panicked
could not parse time: strconv.ParseInt: parsing "2314321432143124223432434": value out of range
Ran 1 of 1 Specs in 0.002 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 0 Skipped我如何正确地测试银杏/果味中的恐慌?
发布于 2022-06-15 04:31:11
从文件中:
如果实际是一个函数,当调用时,
会成功。实数必须是不带参数而不返回结果的函数--任何其他类型的实数都是错误。
注意,实际(传递给Expect的参数)应该是一个函数。
在这种情况下,Gomega将做的是调用该函数并捕捉恐慌,这样它就可以对其进行断言。
若要修复特定示例,请执行以下操作:
var _ = ginkgo.Describe("Format Tests", func() {
ginkgo.Describe("When formatting the date", func() {
ginkgo.It("should panic if the time can't be formatted", func() {
gomega.Expect(func(){
tools.ParseUnixTimeString("2314321432143124223432434")
}).To(gomega.Panic())
})
})https://stackoverflow.com/questions/72624115
复制相似问题