用工厂laravel存储假货

时间:2019-05-21 01:06:42

标签: laravel laravel-5

我正在尝试在删除中使用伪造的存储,但是我不知道如何用工厂模拟上传文件。我的测试:

 $actualCategory = factory(Category::class)->create();

    $this->json(
        'DELETE',
        '/api/category/' . $actualCategory->id
    )->assertStatus(200)
        ->assertJsonStructure([
            'success',
            'data' => [
                'id',
                'name',
                'image_code',
                'updated_at',
                'deleted_at',
                'created_at'
            ]
        ]);

    $this->assertSoftDeleted('categories', [
        'id' => $actualCategory->id,
        'name' => $actualCategory->name
    ]);

    Storage::disk('categories')->assertMissing($actualCategory->image_code);

如果我对此发表评论,则json仍可用于删除照片

我的工厂是

$factory->define(Category::class, function (Faker $faker) {

return [
    'name' => "Category {$faker->firstName}",
    'image_code' => $faker->image('/tmp', 300, 300)
];

});

1 个答案:

答案 0 :(得分:0)

class ExampleTest extends TestCase
{
    public function testAlbumUpload()
    {
        Storage::fake('photos');

        $response = $this->json('POST', '/photos', [
            UploadedFile::fake()->image('photo1.jpg'),
            UploadedFile::fake()->image('photo2.jpg')
        ]);

        // Assert one or more files were stored...
        Storage::disk('photos')->assertExists('photo1.jpg');
        Storage::disk('photos')->assertExists(['photo1.jpg', 'photo2.jpg']);

        // Assert one or more files were not stored...
        Storage::disk('photos')->assertMissing('missing.jpg');
        Storage::disk('photos')->assertMissing(['missing.jpg', 'non-existing.jpg']);
    }
}