I try to test the method index() of the controller. In this method, there's a model.
class UserController extends Controller
{
public function index()
{
return User::all();
}
}
In the test class, I have the following.
class UserControllerTest extends TestCase
{
public function testIndex():void
{
$user = factory(User::class)->make();
$mock = Mockery::mock(User::class);
$mock->shouldReceive('all')->andReturn($user);
$this->app->instance('User', $mock);
$response = $this->json('GET', 'api/users');
dd($response->getContent()); // error : [2002] Connection refused
}
}
When I run the test I have an error with the connection to the database. This is weird because I have mocked the model which would mean that I don't need to establish a connection to the database. How can I solve this error?
Error
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from
userswhereusers.deleted_atis null)