Laravel 11 AssertableJsonString::assertMissingExact

laravel 11 assert missing exact
22 December 2024

Description of the assertMissingExact method in Laravel 11


The assertMissingExact method in the Laravel 11 library provides us with the capability to verify whether a specific value does not exist in a JSON response. This method is particularly useful in unit tests, where the accuracy and correctness of the output data is of high importance.


Let’s consider that you have an API that returns some user-related information in JSON format. Now, you might want to ensure that a specific attribute (for example, email) is not present in the JSON response. By using this method, you can easily achieve this task.


This method gives you the ability to easily and effectively test whether a specific amount is absent in JSON. On the other hand, if you want to confirm that a specific value exists in JSON, the assertSee method would be a better choice.


For instance, if you want to check that the value of email is not present in the JSON response, you can use assertMissingExact as shown below. This way, you can ensure the accuracy and integrity of the API response.


Code Example of assertMissingExact Method


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

$response->assertJsonMissingExact([
'email' => '[email protected]',
]);

Code Explanation


Code 1: $response = $this->get('/api/users/1');
This line creates an HTTP request to the address /api/users/1 and stores the result in the variable $response.

Code 2: $response->assertJsonMissingExact([
'email' => '[email protected]',
]);

This line checks whether the email field with the value [email protected] does not exist in the JSON output.

FAQ

?

What exactly does the assertMissingExact method do?

?

How can I use assertMissingExact?

?

Is there a difference between assertMissingExact and assertSee?

?

Why are JSON tests important in Laravel?