我正在使用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()
,就会触发错误。我可以确认组件在使用时是否正常工作。
2条答案
按热度按时间w80xi6nr1#
错误是你传递了一个与json_encode()函数不兼容的值,我建议如下:
字符串
frebpwbc2#
好吧,我想我找到了答案:数组中的对象可能没有被正确解析,所以如果我在下面修改测试:
字符串
通过分别调用
assetMeta.book
和assetMeta.cover
的set()
,Type错误消失。