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透视挂效果... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成WePoker透视挂效果如何解析、 ...
技术分享,wepoker有透视... 技术分享,wepoker有透视挂吗,菠萝智星德州有挂么绝活儿(一直是有辅助挂)AI 智能体黑科技再上...
切实教程,wepokerplu... 切实教程,wepokerplus德州作弊,智星菠萝德州透视神器法子(往昔真的是有开挂)主打本地运行的...
AI教程,德扑之星的亮点,德扑... 您好,AI教程,德扑之星的亮点,德扑之星透视开挂项目(最初有透视)这款游戏可以开挂的,确实是有挂的,...
玩家必备科普,WePoKer透... 玩家必备科普,WePoKer透视软件怎么用,wpk到底有挂吗要领(起初真的有透视挂)AI 智能体黑科...
玩家必备教程,wepoker发... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
热门推荐,wepokerplu... 您好:wepokerplus辅助透视教程这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都...
解密关于,wpk破解器,菠萝智... 您好:wpk破解器这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
一分钟快速了解,手机德州有没有... 您好,手机德州有没有挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在...
攻略教程,wepoker透视挂... 攻略教程,wepoker透视挂安装,菠萝智星德州透视方法手段(从来是有辅助挂)主打本地运行的 AI ...