← All writing

Test an upload without writing into your real public disk

Laravel 13 / PHP 8.3+Sources checked 2026-09-14

An upload feature test should not leave sample images in the same folder as production-like fixtures. Laravel's fake disk gives the test an isolated storage target and lets it assert the saved path.

Fake the specific disk the feature uses

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

Storage::fake('public');
$file = UploadedFile::fake()->image('avatar.jpg');
// Submit $file to your authenticated upload route.
// Then assert the path returned by that route:
// Storage::disk('public')->assertExists($storedPath);

This is a test setup fragment, not a complete endpoint test. The request still needs an authenticated user, route, and assertions matching your application. Fake the same disk name used by the production code; faking local will not isolate a write explicitly sent to public.

Check the record and the file

A file existing on disk does not prove the database points to it. Assert both the stored model path and disk existence. For a replacement flow, verify the old file is removed only according to the intended retention policy.

Use separate validation tests for invalid MIME content, excessive size, and an absent file. A generated test image exercises the happy path but cannot prove every real image decoder or hosting permission behaves correctly.

Keep one deployment smoke check for actual public delivery. The fake disk intentionally avoids the real filesystem configuration, so it cannot detect a broken storage symlink, incorrect document root, or owner-only file mode on shared hosting. Unit isolation and live delivery checks answer different questions.

Reference

Official documentation.