【发布时间】:2016-08-25 12:21:22
【问题描述】:
我在使用seeResponseMatchesJsonType 命令时遇到问题。
我有一个带有变量名称和值的 json 字符串,在 "escrow_status" => "string|null" 中出现错误。变量 $escrow_status 未初始化,我希望在那里得到 null,但会得到一个错误。
希望有人知道如何解决此问题。
【问题讨论】:
标签: codeception
我在使用seeResponseMatchesJsonType 命令时遇到问题。
我有一个带有变量名称和值的 json 字符串,在 "escrow_status" => "string|null" 中出现错误。变量 $escrow_status 未初始化,我希望在那里得到 null,但会得到一个错误。
希望有人知道如何解决此问题。
【问题讨论】:
标签: codeception
就像@Naktibalda 在评论中所说:您应该将该断言一分为二。有很多方法可以做到,一种是:
$I->seeResponseJsonMatchesJsonPat('$.escrow'); // you need JSON path module; I would expect seeResponseContainsJson could do it, but it appears that it also needs a value...
$I->seeResponseMatchesJsonType(["escrow_status" => "string|null"]);
另一种方法是断言响应是 JSON,将其转换为数组并对该数组进行断言:
$I->canSeeResponseIsJson();
$data = json_decode($I->grabResponse());
$I->assertArrayHasKey('escrow_status', $data); // might need the assert module
我假设你在测试中使用了一个演员类(因此$I)。
【讨论】:
JsonType 不支持可选字段。
如果该字段可能丢失,请不要使用 JsonType 进行检查。
【讨论】: