我正在尝试使用TDD方式开发某些东西,并且遇到了我无法解决的问题。
我正在上传一个已调整大小的图像,并将其存储在数据库中。
当我手动执行操作(像普通用户一样使用浏览器)时,它可以工作,但测试失败。
这是我的控制人:
public function store(Product $product)
{
//$name = request('image')->store('products', 'public');
$name = str_random(50).'.jpg';
$path = storage_path('app/public/products/'.$name);
$image = Image::make(request('image'));
$image->fit(400, 400);
$image->save($path);
$product->photos()->save(new Photo([
'name' => $name
]));
return redirect($product->adminPath());
}
这是我的考试
public function a_user_can_add_photos_to_the_product()
{
$this->withoutExceptionHandling();
$this->signIn();
$product = ProductFactory::create();
Storage::fake('public');
$this->post($product->adminPath('/photos'), [
'image' => UploadedFile::fake()->image('photo.jpg')
])->assertRedirect($product->adminPath());
$product->load(['photos']);
tap($product->photos->first(), function ($photo) {
$this->assertInstanceOf('App\Photo', $photo);
Storage::disk('public')->assertExists($photo->name);
});
$this->assertEquals(1, $product->photos->count());
}
这时测试将失败,但是如果我注释了调整大小部分并在请求上使用存储方法,如下所示:
Tests\Feature\ManageProductPhotoTest::a_user_can_add_photos_to_the_product
Unable to find a file at path [pJ1Zd19XT0aF9fc5W1famc3n7WwxBt3L9MDB0yRJNlVYcWkMwk.jpg].
Failed asserting that false is true.
public function store(Product $product)
{
$name = request('image')->store('products', 'public');
/*$name = str_random(50).'.jpg';
$path = storage_path('app/public/products/'.$name);
$image = Image::make(request('image'));
$image->fit(400, 400);
$image->save($path);*/
$product->photos()->save(new Photo([
'name' => $name
]));
return redirect($product->adminPath());
}
测试通过。
我该如何解决这个问题?