Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database JSON field mismatch in test assertions due to missing attribute casting #629

Open
idehen-divine opened this issue Feb 28, 2025 · 2 comments

Comments

@idehen-divine
Copy link

When testing database assertions with assertDatabaseHas(), JSON fields like variables may cause failures if the attribute is not explicitly cast in the model.

Issue:

Laravel stores JSON as a string, leading to double-encoded values.
This causes assertDatabaseHas() to fail because it expects an exact match.

Steps to Reproduce

Run php artisan test.
The test fails because the JSON field is stored as a string but asserted as an object.

Expected Behavior:

The test should pass because the inserted record matches the expected values, including JSON fields.

Actual Behavior:

The test fails because Laravel stores variables as a string (double-encoded JSON), causing assertDatabaseHas() to look for a mismatched format.

@bhimbho
Copy link
Contributor

bhimbho commented Feb 28, 2025

the test looks okay to me

@idehen-divine
Copy link
Author

idehen-divine commented Feb 28, 2025

@bhimbho the test is not the issue, the issue comes from the model, no cast so it's reading variables as string instead of json

issue:

Image
Image

fix:

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants