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

相关内容

热门资讯

一分钟教会你!hhpoker有... 一分钟教会你!hhpoker有没有辅助,hhpoker德州透视挂,透明挂教程(有挂软件);玩家必备必...
发现玩家!德普软件,佛手大菠萝... 发现玩家!德普软件,佛手大菠萝辅助,wpk教程(有挂软件)是一款可以让一直输的玩家,快速成为一个“必...
新手必备!菠萝辅助器免费版的功... 新手必备!菠萝辅助器免费版的功能介绍,wepoker有辅助器吗,扑克教程(有挂教程);玩家必备必赢加...
一分钟了解!德普之星透视辅助插... 一分钟了解!德普之星透视辅助插件,epoker免费透视脚本,解密教程(有挂攻略);玩家必备必赢加哟《...
热点讨论!德州局hhpoker... 热点讨论!德州局hhpoker,约局吧如何查看是否有挂,教你教程(有挂神器);支持多人共享记分板与复...
一分钟了解!德普之星的辅助工具... 一分钟了解!德普之星的辅助工具介绍,线上德州的辅助器是什么,2025新版总结(有挂神器);支持多人共...
一秒答解!wepoker的辅助... 1、一秒答解!wepoker的辅助器,wepoker轻量版有透视吗,玩家教你(有挂透视)。2、wep...
热点推荐!wejoker辅助软... 热点推荐!wejoker辅助软件视频,hhpoker是真的还是假的,黑科技教程(有挂教程);原来确实...
新手必备!德普之星透视辅助插件... 1、新手必备!德普之星透视辅助插件,wepokerplus开挂,攻略教程(有挂辅助)(UU poke...
总算了解!wpk免费辅助,aa... 总算了解!wpk免费辅助,aapoker辅助器是真的吗,大神讲解(有挂技巧)科技教程也叫必备教程,这...