Unit test for the following code fails:
This is my main code:
package locprovider
import (
"encoding/json"
"fmt"
"net/http"
"api/domain/location"
"api/utils/error"
"github.com/mercadolibre/golang-restclient/rest"
)
const (
getCountryUrl = "https://api.mercadolibre.com/countries/%s"
)
func GetCountry(countryId string) (*location.Country, *error.ApiError) {
response := rest.Get(fmt.Sprintf(getCountryUrl, countryId))
if response == nil || response.Response == nil {
return nil, &error.ApiError {
Status: http.StatusInternalServerError,
Message: fmt.Sprintf("invalid restclient response when trying to get country %s", countryId),
}
}
if response.StatusCode > 299 {
var apiError error.ApiError
if err := json.Unmarshal(response.Bytes(), &apiError); err != nil {
return nil, &error.ApiError {
Status: http.StatusInternalServerError,
Message: fmt.Sprintf("invalid error response when getting country %s", countryId),
}
}
return nil, &apiError
}
var result location.Country
if err := json.Unmarshal(response.Bytes(), &result); err != nil {
return nil, &error.ApiError {
Status: http.StatusInternalServerError,
Message: fmt.Sprintf("error when trying to unmarshal country data for %s", countryId),
}
}
return &result, nil
}
This is my unit test code:
package locprovider
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetCountryRestClientError(t *testing.T) {
// Execution:
country, err := GetCountry("AR")
// Validation:
assert.Nil(t, country)
assert.NotNil(t, err)
assert.EqualValues(t, 500, err.Status)
assert.EqualValues(t, "invalid restclient response when trying to get country AR", err.Message)
}
When I execute this unit test, I run into following error and I am not sure why it is happening.
I have few other unit tests from other go package, and they work fine without this error.
GOROOT=/usr/local/Cellar/go/1.13.3/libexec #gosetup GOPATH=/Users/abc/go-testing #gosetup /usr/local/Cellar/go/1.13.3/libexec/bin/go test -c -o /private/var/folders/r0/f7kkkm9526lgfz6jtgtphyww0000gn/T/___TestGetCountryRestClientError_in_api_provider_locprovider api/provider/locprovider #gosetup /usr/local/Cellar/go/1.13.3/libexec/bin/go tool test2json -t /private/var/folders/r0/f7kkkm9526lgfz6jtgtphyww0000gn/T/___TestGetCountryRestClientError_in_api_provider_locprovider -test.v -test.run ^TestGetCountryRestClientError$ #gosetup flag provided but not defined: -test.v Usage of /private/var/folders/r0/f7kkkm9526lgfz6jtgtphyww0000gn/T/___TestGetCountryRestClientError_in_api_provider_locprovider: -mock Use 'mock' flag to tell package rest that you would like to use mockups. Process finished with exit code 1
go test -mock? - Grigoriy Mikhalkin