LoginSignup
0
0

More than 3 years have passed since last update.

"Test code or tested code did not (only) close its own output buffers"エラーの調査方法

Posted at

Cakephp3でControllerのテストをしていたのですが、次のようなメッセージが表示されました

Test code or tested code did not (only) close its own output buffers

エラーメッセージでコードを検索すると、出力バッファの階数を検査している箇所でエラーになっていることが分かりました。出力バッファを開始したもののきちんと終了できていないことが原因でにエラーになっています。

/vendor/phpunit/phpunit/src/Framework/TestCase.php
private function stopOutputBuffering()
{
    if (\ob_get_level() != $this->outputBufferingLevel) {
        while (\ob_get_level() >= $this->outputBufferingLevel) {
            \ob_end_clean();
        }

        throw new RiskyTestError(
            'Test code or tested code did not (only) close its own output buffers'
        );
    }
    ...
    以下続きます

そこで、テスト対象のコードから ob_start() を探して、終了するまでの間に何か起きていないか調べてました。デバッガを止めながら実行してみると、途中でExceptionを吐いている箇所を発見することができました。

まとめるとTest code or tested code did not (only) close its own output buffersのエラーになった場合は、デバッガでステップ実行しながらExceptionを吐き出している箇所を見つけましょう。ということになります。

バージョン

  • PHPUnit 6.5.14
  • Cakephp 3.7.7
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0