© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•8mo ago•
5 replies
charlie

Test FileUpload

I can't test my FileUpload

I have this test:

Livewire::actingAs($user)
  ->test(FormForm::class, [
      'formType' => FormType::DFD,
      'userForm' => $form,
      'operation' => 'create',
  ])
  ->set('data', [
      'name' => 'Test',
      // 'file' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
    ])
  ->call('createOrEdit');
Livewire::actingAs($user)
  ->test(FormForm::class, [
      'formType' => FormType::DFD,
      'userForm' => $form,
      'operation' => 'create',
  ])
  ->set('data', [
      'name' => 'Test',
      // 'file' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
    ])
  ->call('createOrEdit');


This test PASSES.

If I uncomment the line
'file' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
'file' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')

the test fails on line
->call('createOrEdit')
->call('createOrEdit')

with the following error:
ErrorException: Trying to access array offset on null
at vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:256
at vendor/livewire/livewire/src/Mechanisms/HandleComponents/HandleComponents.php:88
at vendor/livewire/livewire/src/LivewireManager.php:102
at vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php:94
[...]
at vendor/livewire/livewire/src/Features/SupportTesting/Testable.php:252
at vendor/livewire/livewire/src/Features/SupportTesting/Testable.php:219
at tests/Feature/Livewire/FormTest.php:84
ErrorException: Trying to access array offset on null
at vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:256
at vendor/livewire/livewire/src/Mechanisms/HandleComponents/HandleComponents.php:88
at vendor/livewire/livewire/src/LivewireManager.php:102
at vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php:94
[...]
at vendor/livewire/livewire/src/Features/SupportTesting/Testable.php:252
at vendor/livewire/livewire/src/Features/SupportTesting/Testable.php:219
at tests/Feature/Livewire/FormTest.php:84


I tried all the solutions I could find online, without success...
Solution
I found the solution!

  ->set('data.name', 'Test')
  ->set('data.file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf'))
  ->set('data.name', 'Test')
  ->set('data.file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf'))


I got it from a Dan Harrin's answer on github. You have to add
.0
.0
after the field name.

Be careful, the following doesn't work:
  ->set('data', [
      'name' => 'Test',
      'file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
    ])
  ->set('data', [
      'name' => 'Test',
      'file.0' => UploadedFile::fake()->create('file.pdf', 100, 'application/pdf')
    ])
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

#fileupload #test #pest
FilamentFFilament / ❓┊help
13mo ago
How do you test FileUpload input fields?
FilamentFFilament / ❓┊help
3y ago
fileupload
FilamentFFilament / ❓┊help
2y ago
FileUpload
FilamentFFilament / ❓┊help
2y ago