Does a real upload work? If yes why worry about a test.
Nov 2, 2018
5
Level 2
File upload fails validation in tests when creating fake file
Similar to the issue described here but when validating other file types. In my case I am trying to validate pdf files. The rule passes validation in live testing, but fails when creating a fake file using UploadedFile::fake()->create('file.pdf')
Request Class
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'pdf' => ['required', 'file', 'mimes:pdf'],
];
}
Test Class
$pdf = UploadedFile::fake()->create('file.pdf');
$this->json('POST', route('assets.verify', ['pdf' => $pdf]))->dump();
errors
"pdf": array:2 [
0 => "The pdf must be a file."
1 => "The pdf must be a file of type: pdf."
]
I also tried setting the filesize in the create() method, and setting the mime type to application\pdf
Level 6
Your test class is trying to pass in the file attributes as a query of your route and not actually uploading the file.
$pdf = UploadedFile::fake()->create('file.pdf');
$this->json('POST', route('assets.verify'), ['pdf' => $pdf]);
Notice the route() was closed, then your file is passed in the request.
4 likes
Please or to participate in this conversation.