unit-testing - 为什么“mock.ExpectQuery”返回“没有参数”
问题描述
下面是我的单元测试文件:
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
提前致谢。
解决方案
- sqlmock.New() 如何选择数据库?
答:它不会对数据库进行实际调用。它的一个Mock
意思是不是真正的数据库。
- 为什么 mock.ExpectQuery 返回没有参数
答案: 我没有看到对AddLike()
inside的调用TestAddLike(t *testing.T)
。试着把它也写进去:
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)
AddLike() // <- Add the call to actual function here. Before mock.ExpectationsWereMet
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
- 我想检查我的数据库是否增加了
答:你不能。编写单元测试的主要目的是隔离(独立于实际的数据库调用或对某些服务器的 API 调用)函数并根据这些依赖项的输出测试函数的逻辑(例如:DB 抛出一个error
或API
调用返回不同的响应)。调用实际数据库将打破该规则。这就是集成测试发挥作用的地方。
现在,作为单元测试的一部分,您只需检查:
- 如果将正确的
SQL
查询传递给 SQL 模拟 - 返回
mock
DB 的响应后,验证函数的输出是否为expected
推荐阅读
- java - 如何使 Eclipse 格式化程序在类型注释声明中的数组之后放置一个新行?
- vb.net - 我如何以编程方式迭代绑定列表
- python - 查询子字符串的正则表达式
- azure - 从 azure-aks pod 调用具有不同源 IP 地址的外部服务
- apache-spark - spark s3n 支持端点是否类似于 s3a
- javascript - 嵌套数组 - 跟踪数组内对象的位置
- sql - 键和值的 SQL 查询
- java - 根据条件将字符串转换为字符串
- qt - 未显示 Combobox 的完成符
- mongodb - 如何在 go(lang) 中连接到 mlab mongodb 数据库?