miceli miceli - 10 days ago 7
PHP Question

Testing register form fails with password confirmation with Laravel 5 [SOLVED]

I'm trying to test my register form.

I wrote this test :

public function testUserRegistration()
{
$response = $this->call('POST', 'auth/register', array(
'_token' => csrf_token(),
'name' => 'toni',
'username' => 'toni@free.fr',
'password' => 'toni19'
));

// Error The given data failed to pass validation.

$this->assertEquals(200, $response->getStatusCode());
}


When I run this test, it fails with following error :

Error The given data failed to pass validation.

And my controller code :

$this->validate($request, [
'name' => 'required|unique:users',
'username' => 'required|unique:users,username|email|min:3',
'password' => 'required|confirmed|min:5'
], User::getFormMessages());


Thanks for your help.

Answer

I think you miss one input like:

$response = $this->call('POST', 'auth/register', array(
        '_token' => csrf_token(),
        'name' => 'toni',
        'username' => 'toni@free.fr',
        'password' => 'toni19'
        'password_confirmation' => 'toni19'
    ));

And validator:

$this->validate($request, [
    'name' => 'required|unique:users',
    'username' => 'required|unique:users,username|email|min:3',
    'password' => 'required|min:5|confirmed',
    'password_confirmation' => 'required|min:5|same:password'
], User::getFormMessages());

From Laravel docs:

confirmed

The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

Comments