Sang Nguyen
Sang Nguyen

Reputation: 1720

Laravel unit test with upload file can not get file

I'm working on Laravel 5.6.

My code for unit test:

public function testUpload()
{
    Storage::fake('local');

    $this
        ->post(route('upload', ['file' => UploadedFile::fake()->create('file.txt', 1024)]))
        ->assertSuccessful();
}

But in controller $request->file('file') always null.

The route('upload') is correct but dd($request->file('file')) always null and dd($request->file() is empty array.

Does anyone have any idea about this issue?

Upvotes: 1

Views: 1270

Answers (1)

aceraven777
aceraven777

Reputation: 4556

You want to pass the parameters in the 2nd argument of the post function. This is what you want:

public function testUpload()
{
    Storage::fake('local');

    $this
        ->post(route('upload'), ['file' => UploadedFile::fake()->create('file.txt', 1024)])
        ->assertSuccessful();
}

Upvotes: 1

Related Questions