Ana*_*gio 7 php phpunit assert
我正在使用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';
}
Run Code Online (Sandbox Code Playgroud)
hek*_*mgl 21
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"
Run Code Online (Sandbox Code Playgroud)
小智 12
在较新的phpunit 版本中使用此方法:
$this->assertMatchesRegularExpression('/PATTERN/', $yourString);
Run Code Online (Sandbox Code Playgroud)
PHPUnit 不是为了从断言中返回值而设计的。根据定义,断言旨在在失败时中断流程。
如果你需要做这样的事情,你为什么要使用 PHPUnit?使用preg_match
:
$test = preg_match('/find this text/i', $element);
if($test) {
echo 'text found';
}
else {
echo 'text not found';
}
Run Code Online (Sandbox Code Playgroud)