Search code examples
phpunit-testinglaravel-5phpunittdd

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();

Solution

  • 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