测试用例在通过测试用例上传文件时删除原始文件



这是我的测试用例

public function testSelfieFileUpload()
{
    $path = public_path().'/uploads/sample1.jpg';
    $name = 'sample1.jpg';
    $file = new UploadedFile($path, $name, 'image/png', filesize($path), null, true);
    $response = $this->post($this->endpoint, ['file' => $file], $this->setHeaders());
    $response->assertStatus(200)
        ->assertJsonFragment(array("status" => "success"));
}

但问题是它正在从文件夹中删除原始文件,即 sample1.jpg

帮我。我在这里错过了什么吗?虽然测试用例运行良好。

遇到同样的问题时,在查看源代码后,我找不到修复它的标志。

作为解决方法,我在运行测试时创建了一个副本,该副本被删除,但这不再是问题。

$temporaryFilePath = $this->createTempFile($this->testFilePath);
$file = new IlluminateHttpUploadedFile($path, $name, $mime, filesize($path), null, true);
//do your test logic
...
private function createTempFile($path) {
    $tempFilePath = $this->basePath . 'temp.xlsx';
    copy($path, $tempFilePath);
    return $tempFilePath;
}

您可能希望根据需要对其进行优化。

这对

我有用.
您必须同时存储在public_path中,并storage_path相同的文件:

 $filename = storage_path('app/pdfstest/COVID 19.pdf');
 $pdf = new UploadedFile($filename, $name, 'application/pdf');
 $response = $this->post('/gallery', [
   'user_id' => $this->user->id,
   'doc' => [$pdf],
   'from_tab' => 'test'
 ]);
 $temp_pdf = file_get_contents(public_path('pdfstest/COVID 19.pdf'));
 Storage::put('pdfstest/COVID 19.pdf', $temp_pdf);

最新更新