Laravel 的功能测试 JSON API 在附加文件时失败并出现错误

Laravel's functional testing JSON API fails with an error when attaching files

所以我编写了一个功能测试来测试上传功能,但失败并出现以下错误:

1) UploadTest::testValidVideoUpload
Error: Call to a member function filter() on null

/.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:765
/.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:737
/.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:718
/.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:639
/.../tests/ShareTests/UploadTest.php:19

代码是:

public function testValidVideoUpload()
{
    $this->post(route('share.upload'), ['type' => 'video'])
        ->attach($this->test_case_files_path . 'testcase.mp4', 'file')
        ->seeJson(['error' => false]);
}

我正在使用:

编辑:

我在 RESTful 服务上使用这些测试。我们可以使用 attach 方法吗???因为我查看了它的实现,它使用 DOM 解析器和 CSS 选择器来查明该输入名称是否确实存在于响应中!!!!

问题来了。显然(我还不确定,因为文档根本没有说明)您不能在测试 JSON API 时使用 attach 方法。所以这就是我解决问题的方法。而不是

    $this->post(route('share.upload'), ['type' => 'video'],
        ['HTTP_Authorization' => "Bearer " . $token])
        ->attach($this->test_case_files_path . 'testcase.mp4', 'file')
        ->seeJson(['error' => false]);

我用过这个:

$uploadedFile = new \Symfony\Component\HttpFoundation\File\UploadedFile(
            $file, 'testcase.mp4', 'video/mp4', filesize($file), null, true
        );
        $this->response = $this->call('post', route('share.upload'), ['type' => 'video'], [],
            ['file' => $uploadedFile],
            $this->transformHeadersToServerVars([
                'Authorization' => "Bearer " . $token,
                'Content-type' => 'multipart/form-data',
            ]));
        $this->seeJson(['error' => false]);