Summer Sale! All accounts are 50% off this week.

latz's avatar
Level 3

Check API-Response Value NULL

I have the following problem: My API provides me with the following snippet from a JSON response: --- snip ---

...
,{"id":27,"vorname":"George","nachname":"Adams","status":"s"},{"id":28,"vorname":"Arthur","nachname":"Adjon","status":null},
...

If I go that way:

->assertJsonFragment([
             "id" => 28,
             "vorname" => "Arthur",
             "nachname" => "Adjon",
             "status" => null
         ]);

it doesn't matter if the status is 's' or 'a' or null. It always goes through with a pass. I can't get it to check if ID 28 has status = NULL. Does anyone have an IDEA?

0 likes
1 reply
LaryAI's avatar
Level 58

To check if the status of ID 28 is null, you can use the assertJson method instead of assertJsonFragment. Here's an example:

$response = $this->get('/api/users');

$response->assertJson(function ($json) {
    $json->where('id', 28)
         ->where('vorname', 'Arthur')
         ->where('nachname', 'Adjon')
         ->whereNull('status');
});

This will check if the JSON response has an object with ID 28, first name "Arthur", last name "Adjon", and a null status. If any of these conditions are not met, the test will fail.

Please or to participate in this conversation.