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

assertSessionHasErrors can receive an array, as documented:

$this->assertSessionHasErrors([
    'field' => 'Field error message.'
]);

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


You may use the combination of assertStatus and assertJson

...
->assertStatus(422)
->assertJson([
     'errors' => [
          'field' => [
               'Error message'  
          ]
      ]
]);