Sep 21, 2020
0
Level 13
Test validation without HTTP session
I'm picking up some techniques from Jetstream but testing isn't optimal.
Here's my action class and test.
namespace App\Actions;
class UpdateUserDetails
{
public function update(User $user, array $input)
{
Validator::make($input, [
'name' => 'required|min:3|max:50',
...
])->validate();
...
}
}
/** @test */
public function it_has_a_valid_name()
{
try {
(new UpdateUserDetails())->update($user, ['name' => null]);
} catch (ValidationException $e) {
$this->assertNotNull($e->errors('name'));
}
}
This looks hideous. Especially when you want to make multiple assertions per test.
Jetstream tests like this:
/** @test */
public function it_has_a_valid_name()
{
$this->expectException(ValidationException::class);
(new UpdateUserDetails())->update($user, ['name' => null]);
}
My problem with this is that this test also passes if the name is fine but the e-mail is not allowed. It just looks at the exception and not the errors function on that exception.
Are there any methods to make my code a bit cleaner?
Please or to participate in this conversation.