soufyaneyassin's avatar

PHPUnit test laravel

hey everyone i'am trying to run the following test:

 public function a_user_can_register()
    {

    $user = User::factory()->create();
    $response = $this->post('/register', $user);
    $response->assertRedirect('/register-step-2');
    $this->assertDatabaseHas('users', $user);
          
}

but i am getting this error :

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

You can specify a valid test database connection, username, and password in the phpunit.xml. Do not test against your development database.

1 like
bugsysha's avatar

The error you've posted literally says what the problem is. Your database is not accessible by the provided credentials.

I advise you to go to phpunit.xml file and uncomment following lines:

<!-- <server name="DB_CONNECTION" value="sqlite"/> -->
<!-- <server name="DB_DATABASE" value=":memory:"/> -->

So it should look like:

<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
1 like

Please or to participate in this conversation.