PHPUnit自定义断言消息

7

我正在编写一个自定义的PHPUnit断言,但是每当我放置自定义断言时,我看到两个错误消息。

代码如下:

$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.

有没有办法只输出自定义信息并跳过第二个信息?
1个回答

0

我假设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; 
  }
}

稍微有些晚来的派对,但是...那个应该可以工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接