feat(api): 增强 API 测试框架的断言功能
- 修改 ApiCase 和 ApiResponse 结构,新增 Verify 方法以支持自定义校验函数 - 更新 Get、Post、Put、Delete 方法,允许使用 expect 参数进行更灵活的响应断言 - 添加 ExpectResult 方法,支持对响应结果的结构和类型进行后置校验 - 更新文档,详细说明新功能的使用方法和示例,提升测试框架的可用性 - 增强 Swagger 生成逻辑,支持输出新的 expect 和 verifyError 字段
This commit is contained in:
@@ -43,6 +43,67 @@ func nowFunc(that *Context) string {
|
||||
return "NOW()"
|
||||
}
|
||||
|
||||
// ExpectDemoCtr 用于展示 result 结构校验的各种场景
|
||||
var ExpectDemoCtr = Ctr{
|
||||
"string_result": func(that *Context) {
|
||||
that.Display(0, "操作成功")
|
||||
},
|
||||
"number_result": func(that *Context) {
|
||||
that.Display(0, 42)
|
||||
},
|
||||
"float_result": func(that *Context) {
|
||||
that.Display(0, 3.14)
|
||||
},
|
||||
"bool_result": func(that *Context) {
|
||||
that.Display(0, true)
|
||||
},
|
||||
"map_result": func(that *Context) {
|
||||
that.Display(0, Map{
|
||||
"id": 1, "name": "示例商品", "price": 19.9, "in_stock": true,
|
||||
})
|
||||
},
|
||||
"nested_result": func(that *Context) {
|
||||
that.Display(0, Map{
|
||||
"id": 100, "title": "测试订单",
|
||||
"customer": Map{"id": 1, "name": "张三", "phone": "13800138000"},
|
||||
"items": Slice{
|
||||
Map{"id": 1, "goods_name": "苹果", "quantity": 5, "price": 3.5},
|
||||
Map{"id": 2, "goods_name": "香蕉", "quantity": 10, "price": 2.0},
|
||||
},
|
||||
"extra": Map{
|
||||
"delivery": Map{"address": "XX路1号", "fee": 5},
|
||||
},
|
||||
})
|
||||
},
|
||||
"slice_result": func(that *Context) {
|
||||
that.Display(0, Slice{
|
||||
Map{"id": 1, "name": "标签A"},
|
||||
Map{"id": 2, "name": "标签B"},
|
||||
})
|
||||
},
|
||||
"error_demo": func(that *Context) {
|
||||
name := that.Req.FormValue("name")
|
||||
if name == "" {
|
||||
that.Display(3, "名称不能为空")
|
||||
return
|
||||
}
|
||||
that.Display(0, Map{"id": 1, "name": name, "created": true})
|
||||
},
|
||||
"no_expect": func(that *Context) {
|
||||
that.Display(0, Map{"id": 1, "data": "无预期校验"})
|
||||
},
|
||||
"verify_demo": func(that *Context) {
|
||||
name := that.Req.FormValue("name")
|
||||
if name == "" {
|
||||
that.Display(3, "名称不能为空")
|
||||
return
|
||||
}
|
||||
initTestTables(that)
|
||||
id := that.Db.Insert("test_batch", Map{"name": name, "title": "verify_demo", "state": 0})
|
||||
that.Display(0, Map{"id": id, "name": name})
|
||||
},
|
||||
}
|
||||
|
||||
// TestCtr 通用 ORM 测试控制器(数据库无关)
|
||||
var TestCtr = Ctr{
|
||||
"test": func(that *Context) {
|
||||
|
||||
Reference in New Issue
Block a user