阅读(2989)
赞(14)
Laravel 8 测试文件上传
2021-07-08 09:45:51 更新
IlluminateHttpUploadedFile
提供了一个 fake
方法用于生成虚拟的文件或者图像以供测试之用。它可以和 Storage
facade 的 fake
方法相结合,大幅度简化了文件上传测试。举个例子,你可以结合这两者的功能非常方便地进行头像上传表单测试:
<?php
namespace TestsFeature;
use IlluminateFoundationTestingRefreshDatabase;
use IlluminateFoundationTestingWithoutMiddleware;
use IlluminateHttpUploadedFile;
use IlluminateSupportFacadesStorage;
use TestsTestCase;
class ExampleTest extends TestCase
{
public function testAvatarUpload()
{
Storage::fake('avatars');
$file = UploadedFile::fake()->image('avatar.jpg');
$response = $this->json('POST', '/avatar', [
'avatar' => $file,
]);
// 断言文件被存储...
Storage::disk('avatars')->assertExists($file->hashName());
// 断言文件不存在...
Storage::disk('avatars')->assertMissing('missing.jpg');
}
}