All individual subscriptions are 50% off this week only!

Veleous
4 months ago

Unit testing Laravel validation rules

Posted 4 months ago by Veleous

Given the validation rules below:

'field' => 'required|string|max:100|unique:table,column'

I have written feature tests that check each validation rule fails/passes and the response messages for the responses.

Example:

function it_should_fail_validation_if_field_not_provided()
{
  // Arrange

  // Act

  // Assert
}

Is there any reason to write unit tests to confirm each of the valuation rules work in isolation, or am I basically writing tests to confirm something that has already been tested as part of the Laravel framework?

Example:

test it_should_be_invalid_if_field_not_provided()
{
  $model = factory(Model::class)->raw(['field' => '']);

  $v = Validator::make($model, $rules);

  $this->assertTrue($v->fails());
}

Please sign in or create an account to participate in this conversation.