assertMatchesResourceCollectionJsonSchema出现“NULLvaluefound,butanobjectisrequired”错误
创始人
2024-09-20 16:00:54
0

这个错误通常出现在使用PHPUnit测试框架时,检查JSON响应是否符合所期望的架构。通常是因为响应JSON为空或者响应格式不正确导致的。

解决方法为先检查JSON响应是否为空,然后检查响应格式是否正确。可以尝试使用var_dump函数打印结果来调试该问题。

以下示例演示了如何使用PHPUnit测试框架和纯PHP来进行JSON格式检验的示例:

// 在测试框架中使用PHPUnit和assertMatchesResourceCollectionJsonSchema public function testApiEndpointReturnsSomeData() { $client = new GuzzleHttp\Client(); $response = $client->get('http://example.com/api/endpoint');

$data = json_decode($response->getBody()->getContents(), true);
$schema = file_get_contents(__DIR__ . '/schemas/endpoint-collection.json');

$this->assertArrayHasKey('data', $data, 'Response does not have a "data" key.');

$this->assertMatchesResourceCollectionJsonSchema($schema, $data);

}

// 使用纯PHP进行JSON格式检验示例 $jsonResponse = '{ "data": [ { "id": 1, "name": "John" }, { "id": 2, "name": "Jane" } ] }';

$schema = '{ "$schema": "http://json-schema.org/draft-04/schema#", "type": "object", "required" : [ "data" ], "properties" : { "data": { "type": "array", "items": { "type": "object", "required": [ "id", "name" ], "properties": { "id": { "type": "integer" }, "name": { "type": "string" } } } } } }';

$data = json_decode($jsonResponse, true); $schema = json_decode($schema, true);

$validator

相关内容

热门资讯

来一盘!wepoker的辅助器... 来一盘!wepoker的辅助器,wpk透视工作室,真是存在有挂(揭秘有挂)1、玩家可以在线上大神俱乐...
科技分享!pokermaste... 科技分享!pokermaster修改器,wepoker游戏安装教程,其实真的是有挂(有挂规律)1、操...
一分钟了解!德普之星辅助工具如... 一分钟了解!德普之星辅助工具如何打开,wepoker免费脚本,都是是真的有挂(有挂讲解);1、wep...
实测分享!wepokerplu... 实测分享!wepokerplus脚本,aapoker怎么选牌,原来真的是有挂(有挂教学)1、这是跨平...
玩家必备教程!hhpoker可... 玩家必备教程!hhpoker可以控制牌吗,德州局透视脚本,一直存在有挂(有挂技巧)1、让任何用户在无...
如何分辨真伪!wepoker有... 如何分辨真伪!wepoker有辅助吗,hhpoker是真的还是假的,竟然有挂(有挂解惑)1、任何透视...
避坑细节!wepoker透视a... 避坑细节!wepoker透视app下载,HH平台挂,果然是真的有挂(证实有挂)亲,关键说明,透视脚本...
重大通报!wejoker辅助器... 重大通报!wejoker辅助器要钱玩吗,wepoker挂,一直有挂(确实有挂)1、任何透视是真的假的...
一秒答解!hhpoker有没有... 一秒答解!hhpoker有没有外挂,wepoker私人局俱乐部怎么进,果然存在有挂(今日头条)1、超...
重大通报!德州真人透视脚本,h... 重大通报!德州真人透视脚本,hhpoker作弊码,一直有挂(有挂猫腻)1、破解器简单,机器人操作,插...