iguoji
V2EX  ›  PHP

PHP 的单元测试

  •  
  •   iguoji · Jul 8, 2017 · 2296 views
    This topic created in 3302 days ago, the information mentioned may be changed or developed.

    某 api 方法负责从数据库中查询数据,如果有则返回 json 数据,其中 code = 1,如果没有查到数据 code = 0

    测试方法中如何做 [或者] 判断

    现在是

    $res->seeJson(["code" => 1]);

    这样没问题

    但情况是,code 有可能等于 0

    怎么搞?

    1 replies    2017-07-08 16:32:28 +08:00
    linoder
        1
    linoder  
       Jul 8, 2017
    做 [或者] 判断 是什么意思?

    //判断是否为空
    $this->assertEmpty($res);
    //判断是否为字符串
    $this->assertInternalType('string', $res);
    //判断是否合法不为空 Json
    $this->assertEmpty(\json_decode($res, true));
    //判断 Json 串是否 code 为 1
    $this->assertArraySubset(['code' => 1, $res);
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5640 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 340ms · UTC 06:30 · PVG 14:30 · LAX 23:30 · JFK 02:30
    ♥ Do have faith in what you're doing.