php小编鱼仔今天为大家介绍一下Go语言的单元测试。在软件开发中,单元测试是非常重要的一环。通过编写测试用例,我们可以验证代码的正确性,提高代码质量和可维护性。Go语言提供了丰富的测试框架和工具,使得编写和运行单元测试变得更加简单和高效。本文将带领大家了解Go语言的单元测试,包括测试用例的编写、运行和覆盖率的统计。让我们一起来深入了解Go语言的单元测试吧!
运行单元测试时出现主键违规 user_id 错误
我的togglefavourite功能如下
func ToggleTrackToFavourite(c *gin.Context) { userID := GetCurrentUserID(c) trackID, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Track ID"}) return } if err := favourites.CreateTable(db.DB); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create table" + err.Error()}) return } if isInFavorites(userID, trackID) { err = favourites.RemoveFavorite(db.DB, userID, trackID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to remove track from favorites"}) return } c.JSON(http.StatusOK, gin.H{"message": "Track removed from favorites"}) } else { err = favourites.InsertFavorite(db.DB, userID, trackID) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to add track to favorites : " + err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "Track added to favorites"}) } }
func GetCurrentUserID(c *gin.Context) int { userID, exists := c.Get("userID") if !exists { return 0 } if userIDInt, ok := userID.(int); ok { return userIDInt } return 0 }
上述代码的单元测试函数如下
func TestToggleTrackToFavourite(t *testing.T) { db.InitTestDB() if err := users.CreateTable(db.DB); err != nil { t.Fatalf("Failed to create users table: %v", err) } if err := tracks.CreateTable(db.DB); err != nil { t.Fatalf("Failed to create tracks table: %v", err) } if err := favourites.CreateTable(db.DB); err != nil { t.Fatalf("Failed to create favorites table: %v", err) } user := users.User{ Username: "Test", Email: "[email protected]", Password: "123456", } if err := users.CreateUser(db.DB, &user); err != nil { t.Fatalf("Failed to create user: %v", err) } sampleTrack := tracks.Track{ Name: "Test track", Index: 10, Track: "chere.mp4", Image: "image.jpg", Album_ID: 4, Is_Published: true, } if err := tracks.CreateTrack(db.DB, &sampleTrack); err != nil { t.Fatalf("Failed to insert the example track: %v", err) } r := gin.Default() r.PUT("/user/favorite/toggle/:id", ToggleTrackToFavourite) reqAddToFavorites := httptest.NewRequest("PUT", "/user/favorite/toggle/1", nil) wAddToFavorites := httptest.NewRecorder() r.ServeHTTP(wAddToFavorites, reqAddToFavorites) assert.Equal(t, http.StatusOK, wAddToFavorites.Code) assert.Equal(t, `{"message":"Track added to favorites"}`, wAddToFavorites.Body.String()) // reqRemoveFromFavorites := httptest.NewRequest("PUT", "/user/favorite/toggle/1", nil) // reqAddToFavorites.Params = append(reqAddToFavorites.Params, httptest.Param{"id", "1"}) // wRemoveFromFavorites := httptest.NewRecorder() // r.ServeHTTP(wRemoveFromFavorites, reqRemoveFromFavorites) // assert.Equal(t, http.StatusOK, wRemoveFromFavorites.Code) // assert.Equal(t, `{"message":"Track removed from favorites"}`, wRemoveFromFavorites.Body.String()) if _, err := db.DB.Exec("DROP TABLE IF EXISTS favorite"); err != nil { t.Fatalf("Failed to drop the table: %v", err) } if _, err := db.DB.Exec("DROP TABLE IF EXISTS tracks"); err != nil { t.Fatalf("Failed to drop the table: %v", err) } if _, err := db.DB.Exec("DROP TABLE IF EXISTS users"); err != nil { t.Fatalf("Failed to drop the table: %v", err) } }
不知道如何在单元测试中将 GETCURRENTUSER 函数的默认值传递为 1
只想从收藏夹中添加/删除曲目
代码中的用户 ID 似乎存储在处理程序之前的某些身份验证中间件的上下文局部变量中名为“userId”的局部变量中,因此设置它足以进行测试,您可以更改这些行:
r := gin.Default() c.Set("userID", "1") r.PUT("/user/favorite/toggle/:id", ToggleTrackToFavourite)
以上是Go 语言单元测试的详细内容。更多信息请关注PHP中文网其他相关文章!