发现距离自己的上一篇文章已经过去了4个月,感觉好蠢…………
json断言是啥,就是把接口的返回内容当做一个json对象而不是一个文本来处理
优点我找到的有俩,一个是部分内容如果当做普通文本来断言,即使返回的内容跟断言一致了也会判定为错误,我自己就遭遇过这个问题,json断言就没事;二是json断言对返回值的判定更灵活一些,可以做的事情也多。
缺点嘛,学习成本?编写也需要时间,而且只针对json格式。其实如果只是想断言code=200,用响应断言应该是最方便快捷的。
举个栗子:
接口返回值是:
{
"msg": "获取成功",
"data": {
"types": [{
"total": 4,
"data": [{
"signUpDuration": 259200000,
"signUpEndTime": 1583415000000,
"userStatus": 0,
"isLiving": false,
"dateRange": "03-05 19:30",
"courseTypeId": 2,
"picbk": "https://static-image.todoen.com/image/pictur/2020/0225/297a824f-b22c-4d45-95ff-ada6b2379885.png",
"discountPrice": "0.0",
"labelUrl": "",
"labels": ["答题逻辑", "名师助力", "高效提分"],
"showNum": 491,
"intro": "解密雅思听力阅读备考技巧,从此7+不是梦!",
"name": "解锁听力&阅读的8分副本",
"typeId": "2",
"serverCurrentTime": 1583245655102,
"courseId": 1231
},
{
"signUpDuration": 259200000,
"signUpEndTime": 1583501400000,
"userStatus": 0,
"isLiving": false,
"dateRange": "03-06 19:30",
"courseTypeId": 2,
"picbk": "https://static-image.todoen.com/image/pictur/2020/0227/cfd37dc0-c1ad-4cda-a797-3831168445ea.jpg",
"discountPrice": "0.0",
"labelUrl": "",
"labels": ["审题秘诀", "话题拓展", "词汇语法"],
"showNum": 462,
"intro": "名师揭秘雅思口语写作提分诀窍",
"name": "解锁口语&写作的7分副本",
"typeId": "2",
"serverCurrentTime": 1583245655102,
"courseId": 1232
},
{
"signUpDuration": 259200000,
"signUpEndTime": 1583572500000,
"userStatus": 0,
"isLiving": false,
"dateRange": "03-07 16:30",
"courseTypeId": 2,
"picbk": "https://static-image.todoen.com/image/pictur/2020/0302/3fcbe903-e3b3-4ace-8009-722d19ed4098.jpg",
"discountPrice": "0.0",
"labelUrl": "",
"labels": ["新季题型", "高分攻略", "趋势预测"],
"showNum": 297,
"intro": "雅思停考不停学,攻破新题季难题障碍",
"name": "停考期,口语新季高分自救指南",
"typeId": "2",
"serverCurrentTime": 1583245655102,
"courseId": 1254
},
{
"signUpDuration": 259200000,
"signUpEndTime": 1583583300000,
"userStatus": 0,
"isLiving": false,
"dateRange": "03-07 19:30",
"courseTypeId": 2,
"picbk": "https://static-image.todoen.com/image/pictur/2020/0303/0da45ce9-ee47-494a-8b5a-e9ba53d01977.jpg",
"discountPrice": "0.0",
"labelUrl": "",
"labels": ["审题技巧", "立意方法", "高效提分"],
"showNum": 198,
"intro": "姜尧 写作短期提分王 帮你在立意上拔得头筹",
"name": "从作文立意,就让你7分出道",
"typeId": "2",
"serverCurrentTime": 1583245655102,
"courseId": 1255
}],
"pageIndex": 1,
"more": false,
"showMore": 2,
"name": "推荐",
"pageSize": 4,
"typeId": 7
},
{
"total": 1,
"data": [{
"signUpDuration": 259200000,
"signUpEndTime": 1583326800000,
"userStatus": 0,
"isLiving": false,
"dateRange": "明天 19:30",
"courseTypeId": 2,
"picbk": "https://static-image.todoen.com/image/pictur/2020/0225/32a0a9ec-9b69-4080-b065-89b0f6eefef0.jpg",
"discountPrice": "0.0",
"labelUrl": "",
"labels": ["写作逻辑", "方法技巧", "提分思路"],
"showNum": 416,
"intro": "刘薇 雅思天后领衔,全科规划, 助你一站式解决备考难题",
"name": "写作中,考官是如何扣分的?",
"typeId": "2",
"serverCurrentTime": 1583245655102,
"courseId": 1229
}],
"pageIndex": 1,
"more": false,
"showMore": 2,
"name": "写作",
"pageSize": 1,
"typeId": 133
}]
},
"sucess": "A000000"
}
然后,json断言就写:$.data.types[].data[].isLiving,值是false,因为所有的isLiving都是false,所以就通过了
这里有个小坑,就是如果通过了,结果树里面是不显示json断言的,我光看见通过了,还以为有问题呢
下面我就把下图中的挨个试一遍好了
…`$.data.types[*].data[?(@.courseId>1254)].courseId
感觉到处都是坑,图里写的?()是过滤操作,但其实是把唯一一个大于1254的给剩下了,不过想想也是,很多时候筛下去的都是不要的
$.data.types[*].data[0,1].courseId
依然是在给自己挖坑,差点就掉进去了,$.data.types[*]是有2个!所以这里直接返回了3个结果,其中第二组因为只有一节课所以只返回了1个,也就是说,如果你要的是0、1,但其实只有1个,那么就会把0对应的那个返回回来,1的那个就当你没写……
$.data.types[*].data[1:3:2].courseId
这里面,切片的最后一个数字无论是写2还是写几都不起作用,也没有搜到结果,不过倒是搜到了一个切片格式是[start:end] 的,姑且暂时认为不支持step吧,以后用上了再说`