单元测试
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -20,8 +23,17 @@ type PostLoginParam struct {
|
||||
func (c *Controller) PostLogin(ctx *gin.Context) {
|
||||
param := &PostLoginParam{}
|
||||
if err := ctx.ShouldBindJSON(¶m); err != nil {
|
||||
fmt.Println("参数解析失败,请确认参数是否为 JSON")
|
||||
fmt.Println(err)
|
||||
}
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"message": "success",
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Controller) RegisterRoute(gin *gin.Engine) {
|
||||
group := gin.Group(version).Group(group)
|
||||
group.POST("/login", c.PostLogin)
|
||||
}
|
||||
|
||||
// New 获取用户模块的控制器
|
||||
|
||||
20
srv/internal/user/controller_test.go
Normal file
20
srv/internal/user/controller_test.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestLogin(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
app := gin.Default()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("POST", "/api/v1/user/login", nil)
|
||||
app.ServeHTTP(w, req)
|
||||
fmt.Println(w)
|
||||
}
|
||||
Reference in New Issue
Block a user