i have a test case:$response = $this->postJson('api/unit/'.$unit->id.'/import',['file' =>Storage::get('file/file.xlsx')]); $response->assertJsonFragment(['a'=>'b']);my controller:public function import(Request $request, Unit $unit){ $this->validate($request, [ 'file' => 'file|required_without:rows', 'rows' => 'array|required_without:file', 'dry_run' => 'boolean', ]); if ($request->has('rows')) { // } else { $results = $request->file('file'); } return "ok";}but i think my test case is wrong,because when i dd($reuqest->file('file')) in my controller, it return null.So, how can i request file into my controller.please help 解决方案 You can use UploadFile like this:$fileName= 'file.png';$filePath=sys_get_temp_dir().'/'.$fileName;$mimeTypeFile=mime_content_type($filePath);$file=new UploadedFile($filePath, $fileName, $mimeTypeFile, filesize('$filePath'), null, true)$response = $this->postJson('api/unit/'.$unit->id.'/import',['file' =>$file]);$response->assertJsonFragment(['a'=>'b']);with null is The error constant of the upload, true is test modemore detail for Symfony UploadedFile, read this 这篇关于如何将文件注入http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!