【问题标题】:PHPUnit custom assertion messagePHPUnit 自定义断言消息
【发布时间】:2016-03-05 17:19:09
【问题描述】:

我正在编写与 PHPUnit 一起使用的自定义断言,但每当我放置自定义断言时,我都会看到两 (2) 条错误消息。

代码:

$this->_testCase->assertThat(
    $hasMessageCode,
    $this->_testCase->isTrue(),
   "Failed asserting that API response contains a message whose code is {$code}."
);

输出:

1) ApiTest::testFoo
Failed asserting that API response contains an error whose code is REG012.
Failed asserting that false is true.

有什么办法可以只输出 custom 消息而跳过第二条?

【问题讨论】:

    标签: php phpunit


    【解决方案1】:

    我假设 PHPUnit 会立即评估测试然后输出,如果它创建某种稍后评估的列表,这可能不起作用。不是最干净的解决方案,但您可能会弄乱输出缓冲区。

    public function whateverFunctionYouHave($hasMessageCode) {
      ob_start();
      $this->_testCase->assertThat(
        $hasMessageCode,
        $this->_testCase->isTrue(),
       "Failed asserting that API response contains a message whose code is {$code}."
      );
      $out = ob_get_contents();
      ob_end_clean();
      $expectedDefaultMessage = "Failed asserting that false is true.";
      if (strpos($out, $expectedDefaultMessage) !== false) {
        $out = str_replace($expectedDefaultMessage, "");
        echo $out; 
      }
    }
    

    晚会有点晚了,但是...应该工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      相关资源
      最近更新 更多