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私人局透视方法(辅助挂)真是有挂(讲解有挂)1、金币登录送、破产送、升级送、...
教程辅助!hhpoker德州有... 教程辅助!hhpoker德州有挂吗(辅助挂)其实是有挂(有挂方法)1、这是跨平台的hhpoker德州...
六分钟了解!德普之星辅助软件(... 六分钟了解!德普之星辅助软件(辅助挂)真是是有挂(有挂透视)1、进入游戏-大厅左侧-新手福利-激活码...
详细说明!aapoker辅助工... 详细说明!aapoker辅助工具安全吗(辅助挂)一贯真的有挂(有挂猫腻)aapoker辅助工具安全吗...
总算了解!aapoker插件(... 总算了解!aapoker插件(辅助挂)本来是真的有挂(有挂秘诀)1、下载好aapoker插件正确养号...
重磅来袭!pokermaste... 重磅来袭!pokermaster修改器(辅助挂)真是存在有挂(有挂讲解)1、pokermaster修...
六分钟了解!拱趴大菠萝万能挂图... 六分钟了解!拱趴大菠萝万能挂图解(辅助挂)竟然存在有挂(揭秘有挂)暗藏猫腻,小编详细说明拱趴大菠萝万...
总算明白!wpk私人辅助(辅助... 总算明白!wpk私人辅助(辅助挂)一直是真的有挂(有挂辅助)1、下载好wpk私人辅助正确养号方法之后...
玩家必看科普!aapoker真... 玩家必看科普!aapoker真的假的(辅助挂)切实真的是有挂(有挂技巧)1、完成aapoker真的假...
一分钟了解!德普之星辅助器(辅... 一分钟了解!德普之星辅助器(辅助挂)竟然真的是有挂(确实有挂)1.德普之星辅助器 选牌创建新账号,点...