为什么Laravels assertJson在使用has签入根属性时失败?

凉秀策
凉秀策
订阅者
260
文章
0
粉丝
测试交流评论149字数 132阅读0分26秒阅读模式
摘要给出了API调用返回的JSON: { '日期':[ { 'id’:1. } ], '元'{ 'foo”:&引用;巴' }, "左”:{ ...

给出了API调用返回的JSON:

{
  "data": [
     { 
       "id": 1,
     }
  ],
  "meta" {
    "foo": "bar"
  },
  "links": {
    "self": "/"
  }

执行以下代码时:

$response
    ->assertOk()
    ->assertJson(fn (AssertableJson $json) =>
        $json
           ->has('data')
    );

我的测试失败,出现以下错误:

Unexpected properties were found on the root level.
Failed asserting that two arrays are identical.
 --- Expected
 +++ Actual
 -Array &0 ()
 +Array &0 (
 +    1 => 'links'
 +    2 => 'meta'
 +)

我认为断言只会检查响应中是否存在“data”键。
使用时 $json->hasAll(['data',meta',links']) 测试成功。

培训软件测试

 
  • 版权提示:非本站文章仅供存储任何法律责任由作者承担▷违法举报◁▷新闻不符◁▷我要投稿◁
    免责声明:部分内容来自用户上传发布或新闻客户端自媒体如有侵权请反馈站长处理
  • 原创转载:阅读转载说明>>> https://www.playezu.com/zixun/jiaoliu/weishenmelaravels-assertjsonzaishiyonghasqianrugenshuxingshiba.html
匿名

发表评论

匿名网友
确定

拖动滑块以完成验证