tazlan's avatar

Laravel 7x Testing. How to feature test with image posts

I'm quite new to Feature testing and I keep tripping up over some tests. A main part of my application is posting images along with captions, and whatever other text the post requires, this also includes updating that said post or profile.

I've been trying to research how to test whether the post renders along with the caption and post, and the most common approach does not seem to be working for me. I'm not sure what post route to use, or if how I have written it up is correct. However I'll show the areas I beleive to be important below. Please let me know I have left anything out.

I know that the error is falling under the storage::fake and hashname but since i have not used that what would i?

0 likes
6 replies
Sinnbeck's avatar

Show what you have that does not work

1 like
tazlan's avatar

public function test_if_posts_are_created() {

    // First The user is created
    $user = factory(User::class)->make();

    Storage::fake('image');

    //act as user
    $this->actingAs($user)
        // Then we want to make sure a profile page is created
        ->post(route('post.store'), [
            'caption' => 'description of the post',
            'image' => UploadedFile::fake()->image('postimage.jpg')
        ]);

    $imagePath = request('image')->store('uploads', 'public');

    $image = Image::make(public_path("storage/{$imagePath}"))->fit(1200, 1200);

    Storage::assertExists("storage/{$imagePath}" .
        $image);

    $this->assertDatabaseCount('posts', 1);
}
tazlan's avatar
tazlan
OP
Best Answer
Level 1

public function store()

{
    $data = request()->validate([
        'caption' => 'required',
        'image' => ['required', 'image|mimes:jpeg,png,jpg,gif,svg,heic|max:2048']
    ]);

    $imagePath = request('image')->store('uploads', 'public');

    $image = Image::make
    (public_path("storage/{$imagePath}"))
        ->fit(1200, 1200);
    $image->save();

    auth()->user()->posts()->create([
        'caption' => $data['caption'],
        'image' => $imagePath,
    ]);

    return redirect('/profiles/' . auth()->user()->id);
}
Sinnbeck's avatar

And what is the test that is failing and what is the exact error?

1 like
tazlan's avatar

@Sinnbeck InvalidArgumentException

Disk [images] does not have a configured driver.

at vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php:117

113|     {
114|         $config = $this->getConfig($name);
115| 
116|         if (empty($config['driver'])) {

117| throw new InvalidArgumentException("Disk [{$name}] does not have a configured driver."); 118| } 119| 120| $name = $config['driver']; 121| +4 vendor frames 5 tests/Feature/PostControllerTest.php:55 Illuminate\Support\Facades\Facade::__callStatic("assertExists")

tazlan's avatar

@Sinnbeck The test is to check whether or not the posts are being created and stored

Please or to participate in this conversation.