12

PHPUnit を使用して、ページにテキストが存在するかどうかを確認しようとしています。assertRegExp は機能しますが、if ステートメントを使用するとエラーが発生しますFailed asserting that null is true.

$test が null を返すことは理解していますが、テキストが存在する場合に 1 または 0 または true/false を返す方法がわかりません。どんな助けでも感謝します。

        $element = $this->byCssSelector('body')->text();
        $test = $this->assertRegExp('/find this text/i',$element);

        if($this->assertTrue($test)){
            echo 'text found';
        }
        else{
            echo 'not found';
        }
4

3 に答える 3

26

assertRegExp()何も返しません。アサーションが失敗した場合 (つまり、テキストが見つからなかった場合)、次のコードは実行されません。

 $this->assertRegExp('/find this text/i',$element);
 // following code will not get executed if the text was not found
 // and the test will get marked as "failed"
于 2013-09-26T23:39:18.307 に答える
5

PHPUnit は、アサーションから値を返すようには設計されていません。アサーションは、定義上、失敗したときにフローを中断することを意図しています。

このようなことを行う必要がある場合、なぜ PHPUnit を使用するのでしょうか? 使用preg_match:

 $test = preg_match('/find this text/i', $element);

 if($test) {
        echo 'text found';
 }
 else {
        echo 'text not found';
 }
于 2013-09-26T23:52:43.753 に答える