Golang: Mock a multiple http requests using gock

Viewed 2169

I am trying to mock a multiple http requests using gock for unit testing but I am getting this error:

    Error:  Get https://192.64.23.33/q1/status: gock: cannot match any request
    Error:  Get https://194.55.5.6/q1/status: gock: cannot match any request

Code:

func GetEmployee(url string, cli *http.Client) *Response {
    //Do something
    return Response
}

func company(url []string, cli *http.Client) []string {

    for employee := range url {
        resultList := GetEmployee(employee, cli)
        //Process and return an array
        return resultList
    }
}

Unittest:

func TestCompany(t *Testing.t){
  Convey("testing function company",t,func() {
  expected := &Response: {
                Name:     "xyz",
                ID:       1",
                Status:   "active",

            }
  MockCli := &http.Client{}
  testemployees := []string{"192.64.23.33","194.55.5.6"}
  for _, testempl := range testemployees {
        gock.New(fmt.Sprintf("https://%s", testempl)).
            Get("/q1/status").
            Reply(200).
            JSON(expected) 
        gock.InterceptClient(MockCli)
        response, err = company(testemployees, MockCli)

        //some assertions
       }
    })

  }

Unit tests failed when I am passing testemployees := []string{"192.64.23.33","194.55.5.6"} more than one element in a list otherwise if there is only one element then it passes as expected.

How to mock a multiple URLs here so that gock can match a request?

0 Answers
Related