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

相关内容

热门资讯

分享透视!拱趴大菠萝万能挂!推... 分享透视!拱趴大菠萝万能挂!推荐一款辅助挂(有挂解惑)-哔哩哔哩1、用户打开应用后不用登录就可以直接...
了解透视!WePOker有没有... 了解透视!WePOker有没有透视方法!我来教大家辅助插件(有挂神器)-哔哩哔哩;1、这是跨平台的W...
揭幕透视!pokemmo辅助官... 揭幕透视!pokemmo辅助官网!让我来分享经验辅助神器(有挂教程)-哔哩哔哩1、pokemmo辅助...
揭幕透视!epoker底牌透视... 揭幕透视!epoker底牌透视!最新研发辅助攻略(确实有挂)-哔哩哔哩1、起透看视 epoker底牌...
曝光透视!aapoker辅助怎... 曝光透视!aapoker辅助怎么用!交流学习经验辅助工具(有挂解密)-哔哩哔哩1、起透看视 aapo...
关于透视!大菠萝手游辅助!实测... 关于透视!大菠萝手游辅助!实测发现辅助教程(有挂透明挂)-哔哩哔哩1.大菠萝手游辅助 选牌创建新账号...
科普透视!wpk俱乐部有没有辅... 科普透视!wpk俱乐部有没有辅助!分享实测辅助技巧(有挂教学)-哔哩哔哩一、wpk俱乐部有没有辅助游...
普及透视!hhpoker是正品... 普及透视!hhpoker是正品吗!玩家必看科普辅助教程(有人有挂)-哔哩哔哩1、hhpoker是正品...
教你透视!wepoker透视挂... 教你透视!wepoker透视挂底牌!带你了解辅助软件(真实有挂)-哔哩哔哩1、wepoker透视挂底...
解密透视!拱趴大菠萝怎么开挂!... 解密透视!拱趴大菠萝怎么开挂!玩家爆料辅助器(有挂存在)-哔哩哔哩1、游戏颠覆性的策略玩法,独创攻略...