Ginkgo/Gomega恐慌测试失败

huangapple go评论66阅读模式
英文:

Ginkgo/Gomega Panic Test Fails

问题

我正在写一个测试来断言一个函数在无效输入时会发生 panic,但是 Ginkgo 将 panic 记录为失败,而不是预期的通过结果。

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 秒]
[It] should panic if the time can't be formatted
测试发生了 Panic
could not parse time: strconv.ParseInt: parsing "2314321432143124223432434": value out of range
在 0.002 秒内运行了 1 个 Spec
FAIL! -- 0 通过 | 1 失败 | 0 待定 | 0 跳过

如何在 Ginkgo/Gomega 中正确测试 panic?

英文:

I'm writing a test to assert that a function panics on invalid input, but Ginkgo records the panic as a failure instead of a passing result as expected.

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

Which returns (condensed):

•! [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

How do I properly test for panics in Ginkgo/Gomega?

答案1

得分: 6

从文档中可以看到:

> 如果 ACTUAL 是一个函数,在调用时会引发 panic,则成功。ACTUAL 必须是一个不带参数且不返回结果的函数 -- ACTUAL 的任何其他类型都是错误的。

请注意,ACTUAL(传递给 Expect 的参数)应该是一个函数。在这种情况下,Gomega 会调用该函数并捕获 panic,以便可以对其进行断言。

要修复你的特定示例:

var _ = ginkgo.Describe("Format Tests", func() {
    ginkgo.Describe("When formatting the date", func() {
        ginkgo.It("如果时间无法格式化,应该引发 panic", func() {
            gomega.Expect(func(){
                tools.ParseUnixTimeString("2314321432143124223432434")
              }).To(gomega.Panic())
    })
})
英文:

From the documentation:

> succeeds if ACTUAL is a function that, when invoked, panics. ACTUAL must be a function that takes no arguments and returns no result -- any other type for ACTUAL is an error.

Notice that ACTUAL (the argument passed into Expect) is supposed to be a function.
What Gomega will do in this case, is invoke that function and catch the panic so it can assert on it.

To fix your particular example:

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

huangapple
  • 本文由 发表于 2022年6月15日 06:41:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/72624115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定