I've seen several different approaches to testing validation logic.
Some try to test rules in isolation, some just assert the rules definition matches expectations. Others will do feature tests but center the test cases around individual fields.
Our approach is a bit different. We test validation rules within a feature test, but we group the test cases around particular rules.
Those tests are then named after the rule being tested, for example: testUpdateFailsValidationProhibitedIf
.
This approach makes it very easy to scan a whole test suite and keep everything organized. It also cuts down on the number of tests needed to cover all the rules.
In regard to this naming convention, one exception to our rule is with the multipurpose rules like min
and max
.
By multipurpose, I mean that they behave differently depending on the type of data being validated: numeric, string, file, etc.
In these cases, we use the suffixes TooShort
and TooLong
or TooSmall
and TooLarge
for those test names.
Breaking up the these rules into slightly smaller subgroups feels more readable in that particular case.
Here to help,
Joel
P.S. You may have noticed, I really like to obsess about validation. If you want to know more about our approach to validation, check out Mastering Laravel Validation Rules.