Laravel testing using PHP unit - File testing does not support types
P粉087074897
P粉087074897 2023-09-14 23:41:39
0
1
523

I'm using Laravel 10 and Livewire 2. I'm writing a simple test for a component, which is just an upload form. I encountered a mysterious message error:

FAILED  Tests\Feature\Livewire\UploadAssetTest > book can be saved correctly                                                             
Type is not supported

  at vendor/laravel/framework/src/Illuminate/Http/JsonResponse.php:88
     84▕             default => json_encode($data, $this->encodingOptions),
     85▕         };
     86▕ 
     87▕         if (! $this->hasValidJson(json_last_error())) {
  ➜  88▕             throw new InvalidArgumentException(json_last_error_msg());
     89▕         }
     90▕ 
     91▕         return $this->update();
     92▕     }

The test is:

/** @test */
    public function book_can_be_saved_correctly()
    {
        $admin = User::factory()->create();
        $this->actingAs($admin);

        Storage::fake('private');

        $testBook = UploadedFile::fake()->create('private/books/test_book.pdf');
        $testCover = UploadedFile::fake()->image('private/covers/test_image.png');

        $emptyForm = (new UploadBookService())->buildForm();

        $book = array_merge($emptyForm,[
            'title' => 'Title Test',
            'author' => 'Author Test',
            'description' => 'Description Test',
            'publishedAt' => '2022',
            'cover' => $testCover,
            'book' => $testBook,
        ]);

        $component = Livewire::test(UploadAsset::class)
            ->set('assetMeta', $book)
            ->call('save');

            Storage::assertExists('books/test_book.pdf');
            Storage::assertExists('covers/test_cover.png');
    }

With what I've debugged so far, as soon as I call set() in my test, the error is triggered. I can confirm that the component is working when used.

P粉087074897
P粉087074897

reply all(1)
P粉684720851

The error is that the value you pass is incompatible with the json_encode() function, I suggest the following:

$book = array_merge($emptyForm,[
    'title' => 'Title Test',
    'author' => 'Author Test',
    'description' => 'Description Test',
    'publishedAt' => '2022',
    'coverPath' => 'private/covers/test_image.png',
    'bookPath' => 'private/books/test_book.pdf', 
]);

$component = Livewire::test(UploadAsset::class)
    ->set('assetMeta', $book)
    ->call('save');
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!