我正在使用 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()
,就会触发错误。
我可以确认该组件在使用时正在工作。
错误是您传递的值与 json_encode() 函数不兼容,我建议如下: