Apr 30, 2021
4
Level 2
Call to a member function connection() on null at Laravel unit testing
I used the DatabaseMigrations trait at feature tests and It was fine but at unit test, it gives me an error that the connection to sqlite can not be resloved
<?php
namespace Tests\Unit;
use App\Models\Thread;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use PHPUnit\Framework\TestCase;
class ThreadTest extends TestCase
{
use DatabaseMigrations;
/**
* A basic unit test example.
*
* @return void
*/
public function test_each_thread_belongs_to_user()
{
$thread = collect(Thread::factory(1)->create())->first();
$this->assertInstanceOf(User::class, $thread->user);
}
}
error
• Tests\Unit\ThreadTest > each thread belongs to user
Error
Call to a member function connection() on null
at C:\xampp\htdocs\learn-tdd\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1498
1494▕ * @return \Illuminate\Database\Connection
1495▕ */
1496▕ public static function resolveConnection($connection = null)
1497▕ {
➜ 1498▕ return static::$resolver->connection($connection);
1499▕ }
1500▕
1501▕ /**
1502▕ * Get the connection resolver instance.
1 C:\xampp\htdocs\learn-tdd\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1464
Illuminate\Database\Eloquent\Model::resolveConnection()
2 C:\xampp\htdocs\learn-tdd\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1271
Illuminate\Database\Eloquent\Model::getConnection()
Level 55
32 likes
Please or to participate in this conversation.