使用 PHP 单元进行 Laravel 测试 - 文件测试不支持类型
P粉087074897
P粉087074897 2023-09-14 23:41:39
0
1
552

我正在使用 Laravel 10 和 Livewire 2。我正在为一个组件编写一个简单的测试,这只是一个上传表单。我遇到了一个神秘的消息错误:

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▕     }

测试是:

/** @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');
    }

对于我到目前为止所调试的内容,一旦我在测试中调用set(),就会触发错误。 我可以确认该组件在使用时正在工作。

P粉087074897
P粉087074897

全部回复(1)
P粉684720851

错误是您传递的值与 json_encode() 函数不兼容,我建议如下:

$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');
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!