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透视版下载,wepoker脚本下载(透视)技巧教程(有挂详情)1、透视好友!...
透视黑科技!we-poker辅... 透视黑科技!we-poker辅助软件教程,wepoker手机插件(透视)必备教程(有挂攻略)1、we...
透视规律!wepoker有人用... 透视规律!wepoker有人用过吗,wepoker透视破解版(透视)解密教程(有挂插件)1、下载好w...
透视辅助!wepoker免费脚... 透视辅助!wepoker免费脚本弱密码,wepoker怎么拿到好牌(透视)安装教程(有挂介绍)1.w...
透视科技!we poker辅助... 透视科技!we poker辅助器,wepoker辅助插件功能(透视)第三方教程(有挂介绍)1、超多福...
透视安卓版!wepoker免费... 透视安卓版!wepoker免费脚本咨询,wepoker有没有挂(透视)透明教程(有挂脚本)1、wep...
透视教程!wepoker怎么设... 透视教程!wepoker怎么设置盖牌,wepoker提高好牌率(透视)曝光教程(有挂脚本)1、进入到...
透视讲解!购买wepoker模... 透视讲解!购买wepoker模拟器,wepoker辅助工具(透视)大神讲解(有挂揭秘);1、构建自己...
透视讲解!wepoker插件功... 透视讲解!wepoker插件功能辅助器,wepoker轻量版透视系统(透视)揭秘教程(有挂工具);1...
透视教程!wepoker透视功... 透视教程!wepoker透视功能下载,wepoker私人定制透视(透视)攻略方法(有挂介绍)一、we...