Core Laravel Interview Questions and Answers
Laravel is a PHP framework that helps you build web applications. It gives you built-in tools for common tasks like handling web…
A Laravel 11 feature test sends a simulated HTTP request through your application and checks the response without starting a web server or opening a network connection. These HTTP feature tests let you verify how your application handles routing, validation, and responses. When you call Laravel's test helpers, they return a TestResponse object with methods for checking status codes, redirects, JSON, headers, and session data.
Your tests should check the response that each endpoint promises to its clients. A browser-style form redirects back and stores validation errors in the session. An endpoint that expects JSON returns a 422 response with validation errors in the JSON body. The examples below follow Laravel's 11.x HTTP testing documentation; check the documentation for your installed version if your project runs a different Laravel release.
You can call Laravel's get, post, put, patch, and delete helpers to send simulated requests to your application. Because these requests never travel over a real network, your tests do not verify a deployed web server, TLS settings, or a client's network behavior. Laravel also disables CSRF middleware during tests, so an HTTP feature test does not prove that CSRF protection works in production.
Laravel recommends making one request per test method so each test stays focused on a single request and response. A test for a route that requires a title and body looks like this:
public function test_post_form_requires_a_title_and_body(): void
{
$response = $this->post('/posts', [
'title' => '',
'body' => '',
]);
$response
->assertRedirect()
->assertSessionHasErrors(['title', 'body']);
}
This example assumes /posts is a browser-style form route that redirects back when validation fails. assertSessionHasErrors checks for errors stored in the session, and it ignores JSON error content.
Include extra request details whenever the route relies on them to choose a response. You can call withHeaders to supply headers, withSession to add session values, and actingAs to authenticate a user. Pass the same inputs that the route inspects in production.
Laravel formats validation errors to match the incoming request. A traditional form submission redirects back and stores errors in the session. A request that expects JSON receives an HTTP 422 status with the errors in the response body, as described in the Laravel 11.x validation documentation.
Match your test helper to the response format your clients expect. For a JSON route, call a JSON request helper and assert both the status code and the validation errors:
public function test_api_rejects_a_post_without_a_title_or_body(): void
{
$response = $this->postJson('/api/posts', [
'title' => '',
'body' => '',
]);
$response
->assertUnprocessable()
->assertInvalid(['title', 'body']);
}
This example assumes /api/posts expects JSON and validates title and body. assertUnprocessable checks for a 422 status code, and assertInvalid checks that the named fields have validation errors. For a form route, call assertSessionHasErrors. For a JSON route, you can also call JSON-specific assertions like assertJsonValidationErrorFor('title') or assertJsonValidationErrors(['title', 'body']). Laravel provides assertValid() as well, which confirms that validation produced no errors overall or for the specific fields you name. The TestResponse API reference lists all of these assertion methods.
When you test a valid request, check the exact success response your endpoint promises and confirm that the submitted fields passed validation. Check the redirect your form route returns, or check the status code and JSON body your API returns. Because each route and controller defines its own success response, do not assume every Laravel endpoint returns the same status or redirect.
When a test targets a single rule, assert only the field that rule governs. If you check one field per test, you can spot the cause of a failure quickly and keep the test independent of unrelated validation rules. Call assertInvalid when you only care that validation rejected the field, and call session- or JSON-specific assertions when you also want to verify the response format.
Check the full error message only when its exact wording is part of the endpoint's contract. Laravel lets you assert validation errors by field name, and several assertions can match part of a message. That way, your test checks whether validation rejected the input without breaking over minor wording changes.
Input middleware also shapes what reaches the validator. Laravel's default TrimStrings and ConvertEmptyStringsToNull middleware trim strings and turn empty strings into null before validation runs, so an optional field that accepts null usually needs the nullable rule. Write your tests to match your application's middleware and validation rules, especially if custom middleware alters the incoming data. For a closer look at working with validated data, see accessing data after validation.
For nested input, assert the exact error key Laravel generates. Laravel reports validation errors for nested attributes using dot notation, such as users.0.email, so your test should check that key rather than a guessed alternative.
Laravel's JSON helpers include json, getJson, postJson, putJson, patchJson, deleteJson, and optionsJson. Call these helpers whenever an endpoint expects JSON. To check the response body, call assertJson to verify that expected data appears, assertExactJson to require an exact match, or assertJsonPath to check a value at a specific path. If you want to verify important fields while allowing extra fields in the payload, use fluent JSON assertions with etc().
A route may also rely on authentication, headers, session data, or uploaded files during validation. Include those inputs in your test whenever the route checks them. For file upload validation, Laravel's HTTP tests support fake files and fake storage, so you do not need real files or real storage backends. Keep exception handling enabled when you want to verify the error response clients receive. If you disable exception handling to debug a failure, your test changes the response path it checks.
These helpers test incoming requests sent to your Laravel application. To test outgoing requests that your code sends to external services, use Laravel's separate HTTP client fakes feature. See testing outgoing API calls for that workflow.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com