Created
April 21, 2021 05:30
-
-
Save vedashree29296/fb23367b628d57a795687370d98abd2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rest | |
import ( | |
"encoding/json" | |
"net/http" | |
"net/http/httptest" | |
"strconv" | |
"strings" | |
"testing" | |
db "petstore/internal/petstore/repo/postgres" | |
"github.com/labstack/echo/v4" | |
"github.com/stretchr/testify/assert" | |
) | |
func TestGetCategory(t *testing.T) { | |
// Setup for API testing | |
e := echo.New() | |
response := db.Category{} | |
request := httptest.NewRequest(http.MethodGet, "/category", nil) | |
recorder := httptest.NewRecorder() | |
ctx := e.NewContext(request, recorder) | |
ctx.SetPath("/category/:id") | |
ctx.SetParamNames("id") | |
ctx.SetParamValues("1") | |
// Assertions | |
if assert.NoError(t, GetCategory(ctx)) { | |
// check if status code = 200 | |
assert.Equal(t, http.StatusOK, recorder.Code) | |
// check if the response body in not null | |
json.Unmarshal(recorder.Body.Bytes(), &response) | |
assert.NotNil(t, recorder.Body.String()) | |
// check if the id send as a param and id obtained in response are same | |
assert.Equal(t, strconv.Itoa(response.ID), ctx.Param("id")) | |
assert.NotNil(t, response.CategoryName) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment