use assertStatus outside of assertJson
laravel.com/docs/8.x/http-tests#testing-json-apis
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I was working with PHPUnit to test my invoice, so before I implement testing to my app I upgrade my Laravel application from 6 to version 7. and I follow everything in the documentation of the upgrade. in last PHPUnit, it's working but all of my APIs return response like so
return response()->json([
'status' => false,
'message' => "Customer is not exist for this vendor {$vendor_id}"
], 404);
and when I run the test is saying
Tests\Feature\InvoiceTest::salesman_can_store_invoice
Error: Cannot use object of type Illuminate\Http\JsonResponse as an array
C:\xampp\htdocs\backend\app\Http\Controllers\api\v1\InvoiceApiController.php:321
unitTest
$response = $this->withHeaders([
'AuthId' => $driver->id,
'AuthToken' => $driver->authToken,
])->postJson('/api/store_invoice', [$params]);
$response->assertExactJson([
'status' => false,
'message' => "Customer is not exist for this vendor 10"
], 404);
and when I return a response as an array it's working. so I want to know why it's not working when my response looks like I mentioned above?
Please or to participate in this conversation.