gpt4 book ai didi

mysql - 如何为 go-sqlmock 正确设置 Mock Row 和 Query

转载 作者:数据小太阳 更新时间:2023-10-29 03:18:22 24 4
gpt4 key购买 nike

我正在 golang 中设置测试。
我使用 go-sqlmock 来测试 mysql 连接。
但是 sqlmock.NewRowsmock.ExpectQuery 不能正常工作并出现错误。
我想知道如何解决这个错误。

server side: golang
db: mysql
web framework: gin

dao.go

func GetSingleArticleDao(c *gin.Context, db *sql.DB) (util.Article, *sql.Rows) {
id := c.Params.ByName("id")
article := util.Article{}
errArticle := db.QueryRow("SELECT * FROM articles WHERE id = ?", id).Scan(&article.ID, &article.UUID, &article.TITLE, &article.CONTENT)
if errArticle != nil {
panic(errArticle.Error())
}
rows, errImage := db.Query("SELECT image_name FROM images WHERE article_uuid = ?", article.UUID)
if errImage != nil {
panic(errImage.Error())
}

return article, rows
}

dao_test.go

func TestGetSingleArticleDao(t *testing.T) {
db, mock, err := sqlmock.New()

if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}

defer db.Close()

articleMockRows := sqlmock.NewRows([]string{"id", "uuid", "title", "content"}).
AddRow("1", "bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "test", "test")

mock.ExpectQuery("SELECT (.+) FROM articles where id=\\?").
WithArgs("1").
WillReturnRows(articleMockRows)

imageMockRows := sqlmock.NewRows([]string{"article_uuid", "image_name"}).
AddRow("bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "1a90696f-4fe7-48f5-81a5-ca72c129f4b0").
AddRow("bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "3d997272-468f-4b66-91db-00c39f0ef717")

mock.ExpectQuery("^SELECT (.+) FROM images*").
WithArgs("bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c").
WillReturnRows(imageMockRows)

resp := httptest.NewRecorder()
gin.SetMode(gin.TestMode)
ctx, _ := gin.CreateTestContext(resp)

article, imageRows := GetSingleArticleDao(ctx, db)

for imageRows.Next() {
imageName := util.ImageName{}
err := imageRows.Scan(&imageName.NAME)
if err != nil {
panic(err.Error())
}
article.IMAGENAMES = append(article.IMAGENAMES, imageName)
}

expectedArticle := util.Article{
ID: 1,
UUID: "bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c",
TITLE: "test",
CONTENT: "test",
}

imageName1 := util.ImageName{
NAME: "1a90696f-4fe7-48f5-81a5-ca72c129f4b0",
}
expectedArticle.IMAGENAMES = append(expectedArticle.IMAGENAMES, imageName1)

imageName2 := util.ImageName{
NAME: "3d997272-468f-4b66-91db-00c39f0ef717",
}

expectedArticle.IMAGENAMES = append(expectedArticle.IMAGENAMES, imageName2)

assert.Equal(t, expectedArticle, article)
}

我希望 go test -v 运行没有错误。
但实际情况并非如此。
这是错误。

$ go test -v 
=== RUN TestGetSingleArticleDao
--- FAIL: TestGetSingleArticleDao (0.00s)
panic: Query: could not match actual sql: "SELECT * FROM articles WHERE id = ?" with expected regexp "SELECT (.+) FROM articles where id=\?" [recovered]
panic: Query: could not match actual sql: "SELECT * FROM articles WHERE id = ?" with expected regexp "SELECT (.+) FROM articles where id=\?"

goroutine 34 [running]:
testing.tRunner.func1(0xc00022c000)
/usr/local/go/src/testing/testing.go:830 +0x392
panic(0x165a960, 0xc0001bc4a0)
/usr/local/go/src/runtime/panic.go:522 +0x1b5
article/api/dao.GetSingleArticleDao(0xc000214160, 0xc0002180c0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/Users/jpskgc/article/api/dao/dao.go:26 +0x35d
article/api/dao.TestGetSingleArticleDao(0xc00022c000)
/Users/jpskgc/article/api/dao/dao_test.go:92 +0x750
testing.tRunner(0xc00022c000, 0x1788730)
/usr/local/go/src/testing/testing.go:865 +0xc0
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:916 +0x35a
exit status 2
FAIL article/api/dao 0.030s

最佳答案

这是由以下部分引起的

    resp := httptest.NewRecorder()
gin.SetMode(gin.TestMode)
ctx, _ := gin.CreateTestContext(resp)

我应该在 ctx 中添加 "id":"1" 参数。
所以我把那部分改成这样:

    param := gin.Param{"id", "1"}
params := gin.Params{param}
req, _ := http.NewRequest("GET", "/article/1", nil)
var context *gin.Context
context = &gin.Context{Request: req, Params: params}

关于mysql - 如何为 go-sqlmock 正确设置 Mock Row 和 Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com