bwrigley's avatar

Pest test having issue with DB connection

I'm just starting a new laravel 10 project and wanted to give Pest a go. However my initial attempts are not going well.

I have a really simple test to play around:

<?php

namespace Tests\Unit;

use App\Http\Controllers\AvatarController;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;


test('create a user', function(){

    $user = User::factory()->create();

    expect($user->avatar())->toBeNull();

});

this fails with:

  Call to a member function connection() on null

  at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1820
    1816▕      * @return \Illuminate\Database\Connection
    1817▕      */
    1818▕     public static function resolveConnection($connection = null)
    1819▕     {
  ➜ 1820▕         return static::$resolver->connection($connection);
    1821▕     }
    1822▕
    1823▕     /**
    1824▕      * Get the connection resolver instance.

      +13 vendor frames
  14  tests/Unit/AvatarTest.php:13


  Tests:    1 failed (0 assertions)
  Duration: 0.28s

in Model.php if I dd(static::$resolver) it is null

The default ProfileTest.php runs fine with the same factory call

<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ProfileTest extends TestCase
{
    use RefreshDatabase;

    public function test_profile_page_is_displayed(): void
    {
        $user = User::factory()->create();
		//
    }
}

Any help would be brilliant!

0 likes
6 replies
Nakov's avatar
Nakov
Best Answer
Level 73

So either your phpunit.xml connection is not correct, or if you are using any .env.testing file make sure you add something like this:

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

in .env.testing

DB_CONNECTION=sqlite
DB_DATABASE=:memory:

--EDIT

Actually you might be missing the Trait:

<?php
 
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
 
uses(TestCase::class, RefreshDatabase::class)->in('Feature');

in your Pest config. : https://pestphp.com/docs/configuring-tests

3 likes
bwrigley's avatar

@Nakov Thanks so much for your reply!

So you have mostly fixed it for me thank you so much! I had the Trait for 'Feature' but not for 'Unit'

so now php artisan test all runs fine

But now if I try to run just my test php artisan test --filter AvatarTest I get this error:

   ERROR  Cannot find TestCase object on call stack

Location: /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:68

#0 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/TextUI/Application.php(169): PHPUnit\TextUI\TestRunner->run()
#1 /home/vagrant/Code/everyoneinmind/vendor/pestphp/pest/src/Kernel.php(86): PHPUnit\TextUI\Application->run()
#2 /home/vagrant/Code/everyoneinmind/vendor/pestphp/pest/bin/pest(91): Pest\Kernel->handle()
#3 /home/vagrant/Code/everyoneinmind/vendor/pestphp/pest/bin/pest(99): {closure}()
#4 {main}

Caused by: /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/Event/Value/Test/TestMethodBuilder.php:62

#0 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/Runner/ErrorHandler.php(76): PHPUnit\Event\Code\TestMethodBuilder::fromCallStack()
#1 [internal function]: PHPUnit\Runner\ErrorHandler->__invoke()
#2 /home/vagrant/Code/everyoneinmind/vendor/pestphp/pest/overrides/Runner/Filter/NameFilterIterator.php(103): preg_match()
#3 /home/vagrant/Code/everyoneinmind/vendor/pestphp/pest/overrides/Runner/Filter/NameFilterIterator.php(69): PHPUnit\Runner\Filter\NameFilterIterator->setFilter()
#4 [internal function]: PHPUnit\Runner\Filter\NameFilterIterator->__construct()
#5 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/Runner/Filter/Factory.php(53): ReflectionClass->newInstance()
#6 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/Framework/TestSuite.php(386): PHPUnit\Runner\Filter\Factory->factory()
#7 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/Framework/TestSuite.php(272): PHPUnit\Framework\TestSuite->getIterator()
#8 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/Framework/TestSuite.php(320): PHPUnit\Framework\TestSuite->count()
#9 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/Framework/TestSuite.php(340): PHPUnit\Framework\TestSuite->run()
#10 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/Framework/TestSuite.php(340): PHPUnit\Framework\TestSuite->run()
#11 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(63): PHPUnit\Framework\TestSuite->run()
#12 /home/vagrant/Code/everyoneinmind/vendor/phpunit/phpunit/src/TextUI/Application.php(169): PHPUnit\TextUI\TestRunner->run()
#13 /home/vagrant/Code/everyoneinmind/vendor/pestphp/pest/src/Kernel.php(86): PHPUnit\TextUI\Application->run()
#14 /home/vagrant/Code/everyoneinmind/vendor/pestphp/pest/bin/pest(91): Pest\Kernel->handle()
#15 /home/vagrant/Code/everyoneinmind/vendor/pestphp/pest/bin/pest(99): {closure}()
#16 {main}.

but if I run one of the existing laravel test it runs fine art test --filter ProfileTest

Nakov's avatar

@bwrigley

The project I am running Pest on is still Laravel 9, so I haven't experienced the same issue as you did. And PHPUnit is 9.5.27.

I wouldn't mind using Pest even for slight issues like this one, as I know the guys that are maintaining it are very active, and they are actively using it, so either a fix will come very soon, or there is also an opportunity to collaborate to an open source project too :)

but yeah, make sure you weight your pros and cons and decide for yourself :)

Please or to participate in this conversation.