如何在 Laravel 5.2 中测试文件上传
2022-08-30 10:31:25
我尝试测试上传 API,但每次都失败:
测试代码 :
$JSONResponse = $this->call('POST', '/upload', [], [], [
'photo' => new UploadedFile(base_path('public/uploads/test') . '/34610974.jpg', '34610974.jpg')
]);
$this->assertResponseOk();
$this->seeJsonStructure(['name']);
$response = json_decode($JSONResponse);
$this->assertTrue(file_exists(base_path('public/uploads') . '/' . $response['name']));
文件路径为 /public/uploads/test/34610974.jpg
以下是控制器中的我的上传代码:
$this->validate($request, [
'photo' => 'bail|required|image|max:1024'
]);
$name = 'adummyname' . '.' . $request->file('photo')->getClientOriginalExtension();
$request->file('photo')->move('/uploads', $name);
return response()->json(['name' => $name]);
我应该如何在Laravel 5.2中测试文件上传?如何使用方法上传文件?call