【问题标题】:Codeception seeResponseMatchesJsonType commandCodeception seeResponseMatchesJsonType 命令
【发布时间】:2016-08-25 12:21:22
【问题描述】:

我在使用seeResponseMatchesJsonType 命令时遇到问题。

我有一个带有变量名称和值的 json 字符串,在 "escrow_status" => "string|null" 中出现错误。变量 $escrow_status 未初始化,我希望在那里得到 null,但会得到一个错误。

希望有人知道如何解决此问题。

【问题讨论】:

    标签: codeception


    【解决方案1】:

    就像@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)。

    【讨论】:

      【解决方案2】:

      JsonType 不支持可选字段。
      如果该字段可能丢失,请不要使用 JsonType 进行检查。

      【讨论】:

      • 然后用 wich 命令我可以检查 json 字符串中的数据类型?
      • 理想情况下,您应该有 2 个单独的测试 - 一个是字段存在且是字符串,另一个是字段缺失。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2015-06-21
      • 1970-01-01
      • 2015-06-19
      相关资源
      最近更新 更多