joshuajazleung's avatar

Do you create a test for each validation rule of each fields?

I was watching this video https://laracasts.com/series/phpunit-testing-in-laravel/episodes/17?autoplay=true And jeffrey creates single test for each validation rule.

I am wondering if that's the standard way of testing validation rules? Say I have 10 fields and each field has 3 validation rules, that's 30 tests....

Would it be better if I create a test for each field? So only 10 tests in total?

0 likes
3 replies
gcwilliams's avatar

Personally I do the 30 tests. One test for each field and rule. 30 for creation and then another 30 for updating. May be overkill but it takes little time and ensures everything is working as intended.

Cronix's avatar

Seems like kind of a waste to test the framework itself, considering it has plenty of validation tests already. I don't think me testing a required rule will fail anymore than their test of the required rule, etc. Test YOUR code, not the framework.

Look at how many tests there are. They're pretty thorough. Most are in the ValidationValidatorTest.php file, except where they have individual classes, which are all separate.

https://github.com/laravel/framework/tree/5.7/tests/Validation

Tray2's avatar

I always test my validation rules one for each field and rule. However you probably test several fields at once. Something like.

$this->post('/posts', $posts->toArray())
->assertSessionHasErrors(['title' => 'Title is requiered', 'body' => 'Body is required']);

Please or to participate in this conversation.