Same technique is in the docs https://laravel.com/docs/8.x/http-tests#testing-file-uploads
Not sure why you doubt the docs. Official docs are one of the best Laravel features.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have this code to store a new post (it's work as testing manually)
public function store(PostRequest $request, Post $post)
{
$data = $request->validated();
$request->image->store('post_images');
$data['image'] = $request->image->hashName();
$post->create($data);
return redirect()->route('posts.index');
}
My test:
/** @test */
public function an_admin_can_add_a_new_post()
{
$admin = User::factory()->create(['is_admin' => true]);
Storage::fake('public');
$this->actingAs($admin)
->post(route('posts.store'), [
'title' => 'how to write a clean code',
'desc' => 'description of the post',
'image' => $file = UploadedFile::fake()->image('post.jpg'),
'tag_id' => Tag::factory()->create()->id,
]);
Storage::assertExists('post_images/' . $file->hashName());
$this->assertDatabaseCount('posts', 1);
}
The test is passed but I'm have feeling that this is not the correct way for testing image!
Please correct me if I'm wrong!
Please or to participate in this conversation.