2

我刚刚开始学习为 http 请求编写单元测试,我浏览了几个博客,但我不明白如何使用 Ginkgo 编写测试。

func getVolDetails(volName string, obj interface{}) error {
    addr := os.Getenv("SOME_ADDR")
    if addr == "" {
        err := errors.New("SOME_ADDR environment variable not set")
        fmt.Println(err)
        return err
    }
    url := addr + "/path/to/somepage/" + volName
    client := &http.Client{
        Timeout: timeout,
    }
    resp, err := client.Get(url)
    if resp != nil {
        if resp.StatusCode == 500 {
            fmt.Printf("VSM %s not found\n", volName)
            return err
        } else if resp.StatusCode == 503 {
            fmt.Println("server not reachable")
            return err
        }
    } else {
        fmt.Println("server not reachable")
        return err
    }

    if err != nil {
        fmt.Println(err)
        return err
    }
    defer resp.Body.Close()

    return json.NewDecoder(resp.Body).Decode(obj)
}

// GetVolAnnotations gets annotations of volume
func GetVolAnnotations(volName string) (*Annotations, error) {
    var volume Volume
    var annotations Annotations
    err := getVolDetails(volName, &volume)
    if err != nil || volume.Metadata.Annotations == nil {
        if volume.Status.Reason == "pending" {
            fmt.Println("VSM status Unknown to server")
        }
        return nil, err
    }
    // Skipped some part,not required
}

我浏览了这个博客,它准确地解释了我的代码需要什么,但它使用了测试包,我想用 ginkgo 来实现它。

4

1 回答 1

3

看看ghttp包: http: //onsi.github.io/gomega/#ghttp-testing-http-clients https://godoc.org/github.com/onsi/gomega/ghttp

粗略的草图可能如下所示:

import (
    "os"

    . "github.com/onsi/ginkgo/tmp"
    "github.com/onsi/gomega/ghttp"

    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
)

var _ = Describe("GetVolAnnotations", func() {
    var server *ghttp.Server
    var returnedVolume Volume
    var statusCode int

    BeforeEach(func() {
        server = ghttp.NewServer()
        os.Setenv("SOME_ADDR", server.Addr())

        server.AppendHandlers(
            ghttp.CombineHandlers(
                ghttp.VerifyRequest("GET", "/path/to/somepage/VOLUME"),
                ghttp.RespondWithJSONEncodedPtr(&statusCode, &returnedVolume),
            )
        )
    })

    AfterEach(func() {
        server.Close()
    })

    Context("When when the server returns a volume", func() {
        BeforeEach(func() {
            returnedVolume = Volume{
                Metadata: Metadata{
                    Annotations: []string{"foo"}
                }
            }
            statusCode = 200
        })

        It("returns the annotations associated with the volume", func() {
            Expect(GetVolAnnotations("VOLUME")).To(Equal([]string{"foo"}))
        })
    })

    Context("when the server returns 500", func() {
        BeforEach(func() {
            statusCode = 500
        })

        It("errors", func() {
            value, err := GetVolAnnotations("VOLUME")
            Expect(value).To(BeNil())
            Expect(err).To(HaveOccurred())
        })
    })

    Context("when the server returns 503", func() {
        BeforEach(func() {
            statusCode = 503
        })

        It("errors", func() {
            value, err := GetVolAnnotations("VOLUME")
            Expect(value).To(BeNil())
            Expect(err).To(HaveOccurred())
        })
    })
})

我认为您的代码存在一些问题。如果您获得 500 或 503 状态代码,则不一定有,err因此您需要创建并从服务器发回自定义错误。

于 2017-08-03T20:21:13.693 回答