调用查询 [...] 不是预期的

Call to Query [...] was not expected

我想在 go 中测试数据库查询。为此,我尝试使用 sqlmock 库。可悲的是我没有找到解决这个问题的方法

我的测试是

    t.Run("call database", func(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()
        var picArray []string
        technique := "ballpoint"

        mock.ExpectBegin()
        mock.ExpectQuery("SELECT * FROM pictures")

        wanted := picture.Picture{"hurr", "durr", 2020, "hurrdurr"}
        got := GetPicture(db, technique)
        if got != wanted {
            t.Errorf("got %q, wanted %q", got, picArray)
        }
    })
}

我的代码是这样的


func GetPicture(db *sql.DB, style string) picture.Picture {
    rows, err := db.Query("SELECT * FROM pictures")
    if err != nil {
        log.Fatal(err)
    }

    defer rows.Close()
    var (
        name      string
        technique string
        year      int
        fileName  string
    )
    pic := picture.Picture{}
    for rows.Next() {
        err := rows.Scan(&name, &technique, &year, &fileName)
        if err != nil {
            log.Fatal(err)
        }
        log.Println(name, technique, year, fileName)
        pic.Name = name
        pic.Technique = technique
        pic.Year = year
        pic.FileName = fileName
    }
    err = rows.Err()
    if err != nil {
        log.Fatal(err)
    }

    return pic
}

如果我运行测试,我得到以下错误

2020/12/07 20:23:46 call to Query 'SELECT * FROM pictures' with args [], was not expected, next expectation is: ExpectedBegin => expecting database transaction Begin
exit status 1

如果调用了查询,我该如何检查?

好的,所以我不得不打电话给 db.Begin()