1

我正在测试使用 laravel 8 上传文件。

我的控制器

    public function store(Request $request)
    {
        $path = $request->file('file')->store('uploads');
        return $path;
    }

我的测试代码

 /** @test */
    public function check_file_can_be_uploaded()
    {
        Storage::fake('uploads');
        $file = UploadedFile::fake()->image('document.pdf', 300);
        $data = Contact::factory()->make([
            'file' => $file
        ])->toArray();

        $response = $this->post(route('contact.store'), $data);

        Storage::disk('uploads')->assertExists($file->hashName());

    }

我在存储文件夹中看到该文件,但 phpunit 返回无法在路径 [08cnFo56Ce8RIBCwUh7eY8hTZxvdv6atMBdozSaH.pdf] 找到文件。断言 false 为 true 失败。

我哪里错了?

4

1 回答 1

2

我遇到了同样的问题,作为一种解决方法,我使用了以下方法:

self::assertFileExists(storage_path('app/public/1/' . $file->hashName()));

您的经验可能会因存储文件的路径而异

于 2021-03-20T02:21:38.450 回答