AungHtetPaing__'s avatar

UploadedFile is not working

Hi, I'm testing file upload that work actually. It is only failing in testing.

Here is my test

public function testAuthorizedUserCanCreatePost()
    {
        $this->signIn();

        $this->get("/posts/create")->assertStatus(200);

        Storage::fake('local');
        $image = UploadedFile::fake()->image('test_image.jpeg')->size(10);
        
        $post = make('App\Post', ['featured_image' => $image]);

        create('App\Tag', ['id' => 7, 'name' => 'Makayla Wiegand IV']);

        $this->post(route('posts.store'), $post->toArray() + ['tags' => '[{"id":7,"name":"Makayla Wiegand IV"}]'])
            ->assertSessionHasNoErrors();

        Storage::disk('local')->assertExists($image->hashName());
        $this->assertDatabaseHas('posts', ['title' => $post->title]);

        $id = Post::first()->id;

        $this->get("/posts/$id")->assertSee($post->title);
    }

First it fails in image validation 'featured_image' => 'required|image|mimetypes:image/jpeg,image/ png'.

It shows The featured_image must be image and The featured image must be a file of type: image/jpeg, image/png. .

After I changed featured_image to featured_image.*, it passes.

But when i checked it with $request->hasFile('featured_image') it return false. Even i removed that it doesn't save to testing storage disk "local" or whatever it is. What did i miss?

0 likes
2 replies

Please or to participate in this conversation.