下面是我的单元测试文件:
func TestAddLike(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()
rows := sqlmock.NewRows([]string{"id", "title", "body"}).
AddRow(1, "post 1", "hello").
AddRow(2, "post 2", "world")
mock.ExpectQuery("SELECT (.+) FROM testmock").
WillReturnRows(rows)
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
框架:<code>gin
数据库:gorm
我想写一个单元测试..
我有两个问题:
- 如何
sqlmock.New()
选择数据库? - 为什么
mock.ExpectQuery
返回is without argument
提前致谢。