I have written a test that checks if a user is able to upload a file. I use public as my mocking disk. This is my test:
public function testUserCanUploadFile()
{
$this->withoutExceptionHandling();
$this->signIn();
Storage::fake('/public'); //Mock a disk
$file = UploadedFile::fake()->image('test.jpg'); //Upload a fake image.
$assortmentAttributes = Assortment::factory()->raw(); // Use the assortment factory.
$assortmentAttributes['image'] = $file; // Add a additional field in the assortment factory.
$this->post(route('assortments.store'), $assortmentAttributes)->assertRedirect(); // Post the fields to the assortmentcontroller store method.
Storage::disk('/public')->assertExists($file); // Check if the field exists.
}
I have also written code in my controller that makes sure that the file is saved. This is the code in my controller:
public function store(Request $request)
{
Assortment::create([
'file' => $request->file('image')->store('file', '/public'),
'user_id' => $user->id,
'category_id' => $assortment->category->id,
'title' => $assortment->title,
'description' => $assortment->description
]);
if ($request->wantsJson()) {
return response([], 204);
}
return redirect()->route('assortments.show', $assortment->slug)
->with('success','Verzameling is succesvol aangemaakt.');
}
When I run the test I get: Unable to find a file at path. Failed asserting that false is true. I know that this is caused because the controller generates another random file then the one I wanted in my test. But how do I fix this? I have no idea.
I also used assertExist($file->hashName()) in my test. But I deleted that since it gave the same error.
How do I solve this error?