在此测试中,调用此 API 时会执行一个查询。此查询用于选择用户。当我从测试中将此查询注释掉时,有趣的是,这意味着它不是预期的,mock.ExpectationsWereMet
它没有给出任何错误!我是否遗漏了有关此模拟如何工作的某些内容,如果在没有预料到的情况下执行了查询,是否应该给出错误?
func Test(t *testing.T) {
s := suite.Start(t)
s.Mock.MatchExpectationsInOrder(true)
phone := s.GetRandomPhone()
otp_code := s.GetRandomCode()
s.Mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "otps" WHERE phone = $1 AND "otps"."deleted_at" IS NULL`)).
WithArgs(phone).
WillReturnRows(sqlmock.NewRows([]string{"id", "phone", "code", "created_at", "updated_at", "deleted_at"}).
AddRow(1, phone, otp_code, time.Now(), time.Now(), nil))
// This query gets actually called but when I comment it out (meaning not expected)
s.Mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "user" WHERE phone = $1 AND "user"."deleted_at" IS NULL ORDER BY "user"."id" LIMIT 1`)).
WithArgs(phone)
code, response := s.POST(t, "/api/auth/verify-otp/user", nil, &gin.H{"phone": phone, "code": otp_code})
assert.Equal(t, 200, code)
require.Contains(t, response, "access_token")
require.Contains(t, response, "refresh_token")
assert.Equal(t, response["id"], float64(0))
//this does not give any error
require.NoError(t, s.Mock.ExpectationsWereMet())
}