chaudigv's avatar

Livewire s3 file upload unit test

I am following Testing simple S3 uploads but facing a simple issue on assertExists()

Current versions: Laravel 8.20.1 Livewire: 2.3.5 phpunit: 9.3.3

Code

    public function test_resume_file_is_uploaded_successfully()
    {
        $this->actingAs(User::factory()->create());

        Storage::fake('s3');

        $file = UploadedFile::fake()->create('resume.pdf');

        Livewire::test(Upload::class)
            ->set('resume', $file)
            ->call('uploadResume')
            ->assertHasNoErrors();

        $this->assertTrue($user->refresh()->resume()->exists());

        Storage::disk('s3')->assertExists(config('app.asset_url') . '/' . $user->refresh()->resume->file_name); // --> this is where I am facing error
    }

Error

  • Tests\Feature\ResumeTest > resume file is uploaded successfully
  Unable to find a file at path [https://***.cloudfront.net/resume/ZSe4ePnkW8CqilXnXZCb6O0tTJKcWAn00i5J61E9.pdf].
  Failed asserting that false is true.

Code works since I can upload and verify the file on s3 when I test from browser. Just need little guidance when doing same via unit test.

Appreciate your help.

0 likes
3 replies
chaudigv's avatar

Link inside my description is same as what you have provided. And I am using the same code/standard suggested in the article.

Nakov's avatar

oh sorry, I didn't checked that. Just looking at your error, it does not make sense to me to fake an upload but still expect the file to exist on the S3 disk.

Please or to participate in this conversation.