Php Laravel 5.6测试文件上载(在路径[File.txt]处找不到文件)
我是laravel的新手,我可以成功运行我的文件上传程序,它成功上传了我的文件,但单元测试失败,下面是我的代码: UploadTest.phpPhp Laravel 5.6测试文件上载(在路径[File.txt]处找不到文件),php,phpunit,ubuntu-16.04,laravel-5.6,Php,Phpunit,Ubuntu 16.04,Laravel 5.6,我是laravel的新手,我可以成功运行我的文件上传程序,它成功上传了我的文件,但单元测试失败,下面是我的代码: UploadTest.php public function testUploadFile() { $fileSize = 1024; // 1mb $fileName = 'file.txt'; Storage::fake('files'); $response = $this->json('POST', '/webservice/uploa
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);
}
'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'),
],
],
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
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);
}
'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'),
],
],
非常感谢您的帮助。使用Storage::fake('files')
您可以伪造一个名为'files'的所谓磁盘。在filesystems.php中,没有声明名为“files”的磁盘
在FileUploadController中,您将保存到“本地”磁盘上的子目录“files”,因此为了使测试正常工作,只需伪造此磁盘:
Storage::fake('local');
然后,也将此磁盘用于断言:
Storage::disk('local')->assertExists('files/' . $fileName);
在测试环境中,路径将是
storage/framework/testing/disks
,而不是直接低于storage/app
。正是文档中不明显的两点。