php - Laravel 测试商店方法
问题描述
您好,我想在 laravel 中测试一个 store 方法。在 store 方法中有简单的数据和图像。伪造者也生成所有数据和图像,但验证者不接受图像。为什么会这样?伪造者生成正确的图像名称和扩展名。
这是测试用例
public function test_an_authenticated_user_can_add_new_companie ()
{
$companies = factory(Companies::class)->create();
$company = $companies->toArray();
$this->actingAs($this->user);
$response = $this->postJson('/home/companies/create/add', $company);
$response->assertStatus(302);
}
这里是工厂
'name' => $faker->company,
'email' => $faker->email,
'logo' => $faker->image(public_path('img\logos'), 100, 100, null, false),
'website' => $faker->url,
解决方案
Faker 会保存一个临时图像并给你一个文件路径,这不是你想要的。
为了测试上传图像,您应该UploadedImage::fake()
改用。由于您需要伪造,图像类如何通过API
调用来代替。
$data = [
'logo' => UploadedFile::fake()->image('logo.jpg'),
...
];
$response = $this->postJson('/home/companies/create/add', $data);
要检查文件是否实际保存,您可以伪造并断言它已完成。
邮局电话之前。
Storage::fake('public');
通话后的断言。
Storage::disk('public')->assertExists('img\logos\logo.jpg');
推荐阅读
- android - Android Quickblox video call loudspeaker is not automatically on
- asp.net-core - Azure Pipeline Build Using Microsoft.AspNetCore.App 2.2
- java - 捕获 FileNotFoundException 虽然我确实尝试将 input.txt 放在任何目录中?
- c# - 没有try-catch块的C#重新抛出异常
- r - 如何按原样从 tibble 转换为 dataframe?
- javascript - Webpack 横幅插件使用来自另一个插件的文件
- python - 从简历中提取姓名,然后更改简历中的姓名
- python-3.x - 如何使用 Selenium (Python) 抓取多个页面
- php - PHP中的date_format()不返回格式化的日期
- javascript - 在鼠标滚轮后防止鼠标滚轮 1 秒