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');


This test PASSES.

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

the test fails on line
->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


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'))


I got it from a Dan Harrin's answer on github. You have to add .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')
    ])
Was this page helpful?