mock tests
This commit is contained in:
parent
1675c13929
commit
62432a69c1
2 changed files with 1516 additions and 0 deletions
68
internal/merch/handler_test.go
Normal file
68
internal/merch/handler_test.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package merch
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"merch-api/pkg/utils"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func setupTestRouter(mockRepo Repository) *gin.Engine {
|
||||
ut := utils.New()
|
||||
|
||||
tService := newService(mockRepo, ut)
|
||||
tController := newController(tService, ut)
|
||||
|
||||
handler := &Handler{
|
||||
controller: tController,
|
||||
}
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
|
||||
var userId int64 = 789
|
||||
|
||||
router.Use(
|
||||
func(c *gin.Context) {
|
||||
c.Set("userId", userId)
|
||||
c.Next()
|
||||
})
|
||||
|
||||
handler.RegisterRoutes(router.Group(""))
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
func TestCreateLabelSuccess(t *testing.T) {
|
||||
mockRepo := new(MockRepository)
|
||||
mockRepo.
|
||||
On("createLabel", mock.Anything, mock.Anything, mock.Anything).
|
||||
Return(nil)
|
||||
|
||||
router := setupTestRouter(mockRepo)
|
||||
|
||||
label := LabelDTO{
|
||||
Name: "Success",
|
||||
Color: "1",
|
||||
BgColor: "2",
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(label)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := bytes.NewBuffer(payload)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/merch/labels", body)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue