我试图在我的代码中测试异常。File_get_contents当文件不存在时不计算为false
public function testGetFileThrowsException(){
$this->expectException(FileNotFoundException::class);
$file = "db.json";
$this->review->getData($file);
}
“db.json”文件不存在。我的目标是tp有getData()文件来抛出FileNotFoundException。这里是的getData()代码:
public function getData($path){
if(file_get_contents($path) === false){
throw new FileNotFoundException;
}
return $file;
}
的问题是,而不是评估为False,抛出异常,的file_get_contents函数返回:
1) CompanyReviewTest::testGetFileThrowsException
file_get_contents(db.json): failed to open stream: No such file or directory
所以测试没有成功运行。任何想法为什么会发生这种情况?
___有一点:___'$ file'在该方法中不存在?但是如果你将它编码为'if($ file = file_get_contents($ path)=== false){' – RiggsFolly
1)在调试的时候请记住打开错误报告。 2)如果出现错误报告,请检查错误日志。 – RiggsFolly
感谢关于$文件的提示。不幸的是,我的目标是测试如果$ path不存在时是否抛出异常。 –