首先,我使用codeigniter作为我的PHP框架。很抱歉问这个问题,我正在努力理解单元测试的概念。他们说,在开发网站功能之前进行单元测试是一个很好的做法。所以在这里我很想学习这个:
我的方法:
function download()
{
$data = file_get_contents("resources/temp/file.doc");
$name = "new_file.doc";
force_download($name, $data);
}
然后我用testDownload方法创建了一个测试控制器:
public function testDownload()
{
//Call the controller method
$this->CI->download();
//Fetch the buffered output
$out = output();
$this->assertFalse(preg_match('/(error|notice)/i', $out));
}
然后我测试了它:phpunit test.php,并给了我这个:
F[CIUnit] PHP Error: Warning - file_get_contents(resources/temp/file.doc): failed to open stream: No such file or directory File Path: controllers/contract.php (line: 22)
Time: 4 seconds, Memory: 7.50Mb
There was 1 failure:
1) ContractTest::testDownload
Failed asserting that 0 is false.
我只是想测试一下它是否有正确的路径。或者我真的需要一个下载文件的测试脚本吗?对不起,我只是感到困惑。
"没有这样的文件或目录"的原因:
当您使用PHPUnit进行测试时,当前的工作目录将相对于您运行PHPUnit的目录。但在代码中,您将拥有相对于Web服务器目录的writtern。
顺便说一句,单元测试应该测试您编写的代码。在单元测试中测试外部资源似乎不是一个好主意。
可能是因为0只是一个falsy值,而不是字面意义上的false,而assertFalse使用===。请改用assertEquals或其他方法来检查0。