1 回答

TA貢獻1906條經驗 獲得超3個贊
在為測試創建新的時,您需要傳遞true
參數:$test
UploadedFile
new?UploadedFile(__DIR__?.?"/test_png.png",?'test_png.png',?null,?null,?true)
在這里您可以找到構造函數定義:
/**
?* @param bool? ? ? ? $test? ? ? ? ?Whether the test mode is active
?*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Local files are used in test mode hence the code should not enforce HTTP uploads
?*/
public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, bool $test = false)
雖然我不明白為什么要使用真實圖像進行此測試,但 Laravel 提供了一種內置方式來輕松測試文件上傳。
從文檔:
Storage facade 的 fake 方法允許您輕松生成一個假磁盤,結合 UploadedFile 類的文件生成實用程序,大大簡化了文件上傳的測試。
因此,您的測試可以簡化為以下內容:
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
/** @test */
public function it_can_upload_image()
{? ? ? ??
? ? Storage::fake();
? ? $this->post('/media', ['media' => UploadedFile::fake()->image('test_png.png')])
? ? ? ? ->assertStatus(200);
}
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報