作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我是 laravel 的新手,我可以成功运行我的文件 uploader ,它成功上传了我的文件,但是单元测试失败了,这是我的代码:
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);
}
文件上传 Controller
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
]);
}
文件系统.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'),
],
],
非常感谢帮助,谢谢。
最佳答案
使用 Storage::fake('files')
,您可以伪造一个名为“files”的所谓磁盘。在您的 filesystems.php 中没有磁盘声明为"file"。
在您的 FileUploadController 中,您正在保存到“本地”磁盘上的子目录"file",因此为了使您的测试正常进行,只需伪造此磁盘:
Storage::fake('local');
然后也使用此磁盘进行断言:
Storage::disk('local')->assertExists('files/' . $fileName);
在测试环境中,路径将是storage/framework/testing/disks
,而不是直接低于storage/app
。
关于php - Laravel 5.6 测试文件上传(无法在路径 [file.txt] 找到文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49166927/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!