Skip to content

How to test validation errors throw exact error and message in laravel unit tests

How to test specific validation errors in php unit thrown in validation error ? with below code we could check session has errors, but not the exact error

$this->assertSessionHasErrors();

Advertisement

Answer

Got the answer

    $errors = session('errors');
    $this->assertSessionHasErrors();
    $this->assertEquals($errors->get('name')[0],"Your error message for validation");

$errors is MessageBag object which stored in laravel session when validation error thrown using $errors->get(‘name’) you could see all the validation errors as an array

User contributions licensed under: CC BY-SA
4 People found this is helpful