作者:玉米的跟屁虫 | 来源:互联网 | 2022-12-25 11:15
我是laravel的新手,我可以成功运行我的文件上传器,它成功上传我的文件,但单元测试失败,这是我的代码:
UploadTest.php
public function testUploadFile()
{
$fileSize = 1024; // 1mb
$fileName = 'file.txt';
Storage::fake('files');
$respOnse= $this->json('POST', '/webservice/upload', [
'file' => UploadedFile::fake()->create($fileName, $fileSize)
]);
Storage::disk('files')->assertExists($fileName);
Storage::disk('files')->assertMissing($fileName);
}
FileUploadController
public function upload(Request $request)
{
$file = $request->file('file');
if ($file == null) {
return view('fileupload',
['submitClickedMsg' => "Please select a file to upload."]);
}
$path = $file->storeAs('files', $file->getClientOriginalName(), 'local');
return response()->json([
'path' => $path
]);
}
filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
],
],
非常感谢帮助,谢谢.
1> 小智..:
随着Storage::fake('files')
你假一所谓盘命名为"文件".在您的filesystems.php中没有声明名为'files'的磁盘.
在FileUploadController中,您将保存到"本地"磁盘上的子目录"files",因此为了使您的测试工作,只需伪造此磁盘:
Storage::fake('local');
然后使用此磁盘进行断言:
Storage::disk('local')->assertExists('files/' . $fileName);
在测试环境中,路径将是直接storage/framework/testing/disks
而不是storage/app
直接在下面.
正是这两点在文档中并不明显.