Interview frequency: …
Short answer
Use methods such as $this->getJson() or postJson(), and check results with assertions such as assertStatus(200), assertJsonPath(), and assertJsonValidationErrors().
Explanation
Laravel provides a built-in testing environment that simulates complete HTTP requests without an external server. JSON assertions let you check specific response content, including expected fields, missing private details with assertJsonMissing(), and suitable errors for malformed input.
Code example
public function test_can_create_user(): void {
$response = $this->postJson('/api/users', [
'name' => 'Sara',
'email' => '[email protected]'
]);
$response->assertStatus(201)
->assertJsonPath('data.name', 'Sara');
}Common mistakes
- Using $this->post() instead of $this->postJson() in API tests, preventing Laravel from treating the request as JSON.