We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
During testing with the Laravel framework, you often want to check if a database contains a record which contains a JSON string (especially if you are using JSON columns in your database).
You might have found that using json_encode
doesn't work:
$this->assertDatabaseHas(
'backups',
[
'tries' => 1,
'status' => BackupStatus::SUCCESS,
'message' => null,
'document_id' => $document->id,
'destinations' => json_encode(['a', 'b']),
]
);
Due to a difference in the way the JSON string is encoded, the check will fail (.
To fix it, you simply need to use $this->castAsJson
instead (API documentation can be found here:
$this->assertDatabaseHas(
'backups',
[
'tries' => 1,
'status' => BackupStatus::SUCCESS,
'message' => null,
'document_id' => $document->id,
'destinations' => $this->castAsJson(['a', 'b']),
]
);
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.