首页 > 解决方案 > 带有上传文件的Laravel单元测试无法获取文件

问题描述

我正在开发 Laravel 5.6。

我的单元测试代码:

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

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

$request->file('file')但总是在控制器中null

route('upload')是正确的,但dd($request->file('file'))始终是空数组。nulldd($request->file()

有人对这个问题有任何想法吗?

标签: laravellaravel-5

解决方案


您想在post函数的第二个参数中传递参数。这就是你想要的:

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

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

推荐阅读