feat(testing): 新增 AnyMsg 方法以跳过错误消息断言

- 在 Api 和 ApiCase 中新增 AnyMsg 方法,允许在状态不为 0 时跳过错误消息的断言,适用于动态内容的用例。
- 更新文档说明,介绍 AnyMsg 的用法及其链式调用示例。
- 在示例测试中添加 AnyMsg 的使用案例,展示其在处理动态错误消息时的便利性。
This commit is contained in:
2026-07-22 04:38:38 +08:00
parent f53d406110
commit ec9d25fef6
3 changed files with 21 additions and 1 deletions
+15 -1
View File
@@ -189,6 +189,13 @@ func (a *Api) Note(note string) *ApiCase {
return c
}
// AnyMsg 跳过错误 msg 断言(仅断言 status),用于响应 msg 含网络错误等动态内容的用例
func (a *Api) AnyMsg() *ApiCase {
c := a.newCase()
c.skipMsg = true
return c
}
// DB 获取数据库实例(在测试事务内)
func (a *Api) DB() *HoTimeDB {
return &a.app.Db
@@ -221,6 +228,7 @@ type ApiCase struct {
note string
verifyFn func(a *Api) error
bindCase string // 绑定的单接口验收用例名(FromCase)
skipMsg bool // AnyMsg:跳过错误 msg 断言(用于网络错误等动态消息)
}
// FromCase 在已有 ApiCase 上绑定验收用例名(若尚未加载模板则加载)
@@ -300,6 +308,12 @@ func (c *ApiCase) Note(note string) *ApiCase {
return c
}
// AnyMsg 跳过错误 msg 断言(仅断言 status),可继续链式
func (c *ApiCase) AnyMsg() *ApiCase {
c.skipMsg = true
return c
}
// Verify 设置请求后的自定义校验函数(如查库验证数据状态)
// 函数返回 nil 表示校验通过,返回 error 则用例失败并记录错误原因
func (c *ApiCase) Verify(fn func(a *Api) error) *ApiCase {
@@ -343,7 +357,7 @@ func (c *ApiCase) execute(method, desc string, expectStatus int, expectArgs ...i
hasExpectResult = true
}
}
if expectStatus != 0 && expectMsg == "" {
if expectStatus != 0 && expectMsg == "" && !c.skipMsg {
expectMsg = desc
}