Ginkgo/Gomega恐慌测试失败

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

Ginkgo/Gomega Panic Test Fails

问题

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

  1. func ParseUnixTimeString(unixTimeString string) time.Time {
  2. i, err := strconv.ParseInt(unixTimeString, 10, 64)
  3. if err != nil {
  4. panic(fmt.Sprintf("could not parse time: %s", err.Error()))
  5. }
  6. return time.Unix(i, 0)
  7. }
  8. func TestFormat(t *testing.T) {
  9. gomega.RegisterFailHandler(ginkgo.Fail)
  10. ginkgo.RunSpecs(t, "Format Suite")
  11. }
  12. var _ = ginkgo.Describe("Format Tests", func() {
  13. ginkgo.Describe("When formatting the date", func() {
  14. ginkgo.It("should panic if the time can't be formatted", func() {
  15. gomega.Expect(
  16. tools.ParseUnixTimeString("2314321432143124223432434")).To(gomega.Panic())
  17. })
  18. })
  19. })

返回结果(简化):

  1. •! [PANICKED] [0.002 秒]
  2. [It] should panic if the time can't be formatted
  3. 测试发生了 Panic
  4. could not parse time: strconv.ParseInt: parsing "2314321432143124223432434": value out of range
  5. 在 0.002 秒内运行了 1 个 Spec
  6. 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.

  1. func ParseUnixTimeString(unixTimeString string) time.Time {
  2. i, err := strconv.ParseInt(unixTimeString, 10, 64)
  3. if err != nil {
  4. panic(fmt.Sprintf("could not parse time: %s", err.Error()))
  5. }
  6. return time.Unix(i, 0)
  7. }
  8. func TestFormat(t *testing.T) {
  9. gomega.RegisterFailHandler(ginkgo.Fail)
  10. ginkgo.RunSpecs(t, "Format Suite")
  11. }
  12. var _ = ginkgo.Describe("Format Tests", func() {
  13. ginkgo.Describe("When formatting the date", func() {
  14. ginkgo.It("should panic if the time can't be formatted", func() {
  15. gomega.Expect(
  16. tools.ParseUnixTimeString("2314321432143124223432434")).To(gomega.Panic())
  17. })
  18. })

Which returns (condensed):

  1. •! [PANICKED] [0.002 seconds]
  2. [It] should panic if the time can't be formatted
  3. Test Panicked
  4. could not parse time: strconv.ParseInt: parsing "2314321432143124223432434": value out of range
  5. Ran 1 of 1 Specs in 0.002 seconds
  6. 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,以便可以对其进行断言。

要修复你的特定示例:

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

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:

  1. var _ = ginkgo.Describe("Format Tests", func() {
  2. ginkgo.Describe("When formatting the date", func() {
  3. ginkgo.It("should panic if the time can't be formatted", func() {
  4. gomega.Expect(func(){
  5. tools.ParseUnixTimeString("2314321432143124223432434")
  6. }).To(gomega.Panic())
  7. })
  8. })

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:

确定