In this episode, we explore a handy new feature in Laravel 12 that makes your form validation tests more robust: the assertOnlyInvalid
method.
We kick off with a basic Register controller that does some simple name and email validation. Then, we set up a typical test to check if the email validation is working. The usual way is to use assertInvalid
on the email field, and everything seems fine—until we intentionally introduce another validation error on the name field. Surprisingly, the test still passes, and that's a classic false positive because the test doesn't specifically check which fields failed.
To solve this, Laravel 12 introduces assertOnlyInvalid
. We update the test to use this method, specifying that only the email should be invalid. Now, if something else (like the name) also fails validation, the test will catch it and fail, helping us avoid those silent errors.
Finally, we fix our extra validation rule, rerun the test, and watch it pass for the right reason. This episode is all about making your validation tests more precise and trustworthy!