I am trying to unit test various custom FormRequest
objects. I found solutions that:
- Suggest using the
$this->call(…)
method and assert theresponse
with the expected value (link to answer). This is overkill, because it creates a direct dependency on Routing and Controllers. - Taylor’s test, found in
laravel/framework/tests/Foundation/FoundationFormRequestTest.php
. There is a lot of mocking and overhead done there.
I am looking for a solution where I can unit test individual fields (independent of other fields in the same request).
Sample FormRequest:
public function rules()
{
return [
'first_name' => 'required|between:2,50|alpha',
'last_name' => 'required|between:2,50|alpha',
'email' => 'required|email|unique:users,email',
'username' => 'required|between:6,50|alpha_num|unique:users,username',
'password' => 'required|between:8,50|alpha_num|confirmed',
];
}
Desired Test:
public function testFirstNameField()
{
// assertFalse, required
...
// assertTrue, required
...
// assertFalse, between
...
}
public function testLastNameField()
{
...
}
How can I unit test (assert) each validation rule of every field in isolation and individually?
Aucun commentaire:
Enregistrer un commentaire