Stop manually testing your forms. Learn how to use Laravel's testing suite to automate validation checks, simulate authenticated users, and ensure data integrity.
Previously in this course, we covered the Introduction to Testing: Build Confident Laravel Apps, where we set up our environment and performed basic status code checks. Now that you have the foundation, we’ll move into the core of any application: testing forms and validation.
Manual testing is a trap. Every time you change a validation rule or update a form field, you risk breaking your application. Automation is your safety net, ensuring that your logic holds up under pressure.
When we test a form submission, we are essentially simulating a user filling out an HTML form and hitting "Submit." In Laravel, we use the post() method to simulate this request.
To verify success, we need to check two things: the HTTP response (did we get redirected?) and the database state (did the data actually save?).
Consider a TaskController that handles storing new tasks. Here is how we test that a valid task is created:
PHPpublic function test_user_can_create_a_task() { $user = User::factory()->create(); $this->actingAs($user) ->post('/tasks', [ 'title' => 'Learn Laravel Testing', 'description' => 'Master form validation tests', ]) ->assertRedirect('/tasks'); $this->assertDatabaseHas('tasks', [ 'title' => 'Learn Laravel Testing', 'user_id' => $user->id, ]); }
actingAs for AuthenticationThe actingAs() method is a powerful helper that authenticates a specific user for the duration of the test. Since our Task Manager app requires users to be logged in to create tasks, this is mandatory. Without it, the application would redirect the user to the login page, and our assertRedirect('/tasks') would fail.
Just as important as testing that things work is testing that they fail correctly. If a user submits a task without a title, we expect the application to stop them and return them to the form with an error.
Laravel makes this easy with assertSessionHasErrors().
PHPpublic function test_task_creation_requires_a_title() { $user = User::factory()->create(); $this->actingAs($user) ->post('/tasks', [ 'title' => '', #6A9955">// Empty title 'description' => 'This should fail', ]) ->assertSessionHasErrors(['title']); $this->assertDatabaseCount('tasks', 0); }
Notice the assertDatabaseCount check. This is a crucial "negative test." It confirms that even though the request was sent, the application correctly prevented the record from entering your database.
Now it's your turn to advance the project. Open your tests/Feature/TaskTest.php file and follow these steps:
test_task_description_is_optional.actingAs() to log in as a user.POST request to /tasks with only a title (no description).null description.This exercise forces you to consider which fields are truly required versus those that are optional in your business logic.
Even experienced engineers hit these snags when starting with automated testing:
post(), Laravel automatically handles this for you. If you ever find yourself getting a 419 Page Expired error in your tests, ensure your route is in routes/web.php and not routes/api.php.Illuminate\Foundation\Testing\RefreshDatabase trait at the top of your test class. This ensures that every test starts with a fresh, empty database, preventing data from one test from leaking into another.required validation rule works—the framework developers already did that. Test your business logic, such as your specific validation rules or custom logic in your controller.Testing forms and validation is about building confidence. By using actingAs() to handle authentication, post() to simulate submissions, and assertSessionHasErrors() to verify your constraints, you create a suite that protects your app from regressions.
Remember, these tests act as documentation for your code. If a future developer wonders if a task description is required, they can simply look at your test file to find the answer.
Up next: We will explore how to ensure data integrity across multiple operations using Database Transactions.
Learn to customize Laravel validation error messages to provide clear, helpful feedback to your users. Master field-specific errors and language files.
Read moreLearn how to use Laravel validation to ensure data integrity. Discover how to apply rules, handle failures, and display error messages in your Blade views.
Testing Forms and Validation
Managing Assets in Production
Task Manager: Deployment Preparation