Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Brainmaniac's avatar

Can't use sqlite in memory DB for pest test

I am about to start testing with Pest! It seems great BUT.. I am following instructions and have successfully been running the first test tests 😅 - But when I go to my phpunit.xml and uncomments the following rows to start use in memory sqlite db the test tests do not pass any more:

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

The error I get after uncommenting and running the test is a humungous stacktrace but the error is:

   PASS  Tests\Unit\ExampleTest
  ✓ basic test

   FAIL  Tests\Feature\ExampleTest
  ⨯ it has welcome page

  ---

  • Tests\Feature\ExampleTest > it has welcome page
  Expected response status code [200] but received 500.
  
  The following exception occurred during the request:
  
  PDOException: SQLSTATE[HY000]: General error: 1 no such table: categories in /Users/<My_user>/<my_project>/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:82
  Stack trace:
  0 /Users/<My_user>/<my_project>/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php(82): PDO->prepare('select * from "...', Array)

The 1 no such table: categories in makes me think that there might be something wrong with the migration of the test db?

1 like
8 replies
tykus's avatar

Are you using the RefreshDatabase or LazilyRefreshDatabase trait in the Test class?

https://pestphp.com/docs/underlying-test-case

Or, is there something on the welcome page that requires you to have Category model instances (e.g. a menu)? Do you use creator/composer/View::share in a ServiceProvider to provide data to your views?

1 like
Brainmaniac's avatar

@tykus Hi! Sorry new to pest.

Are you using the RefreshDatabase or LazilyRefreshDatabase trait in the Test class?

How do I know this?

Brainmaniac's avatar

@tykus a just saw it srry. Yes I have now made sure I refresh the db before each test... I think you might be right that I am missing the categories... I will make a factory for it. Thanks for the help

tykus's avatar

@Brainmaniac

I think you might be right that I am missing the categories

That's not what I was suggesting; the error message says there is no database table

no such table: categories

If you examine the stack trace further, you will see what application code is initiating the query; that will point us to the reason for the error message.

Brainmaniac's avatar

@tykus aa okok cotcha. I did find it after all. There was one of my migrations that did stuff that was not supported in sqlite but is in mysql as I am running normally.. I probably should not run sqlite as test db (i know) but I kinda just want to get this started for now. I modified the migration and I believe that the migration works now.

But I get another issue when I try to create an instance with my factory:

<?php

use App\Models\MyModel;
use Illuminate\Foundation\Testing\RefreshDatabase;

// Uses the given trait in the current file
uses(RefreshDatabase::class);

beforeAll(function(){
    MyModel::factory()->create();
});

it('has welcome page')->get('/')->assertStatus(200);

The error:

  • Tests\Feature\ExampleTest > it has welcome page
   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [config] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:879
    875â–• 
    876â–•         try {
    877â–•             $reflector = new ReflectionClass($concrete);
    878â–•         } catch (ReflectionException $e) {
  ➜ 879▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    880â–•         }
    881â–• 
    882â–•         // If the type is not instantiable, the developer is attempting to resolve
    883â–•         // an abstract type such as an Interface or Abstract Class and there is

      +19 vendor frames 
  20  tests/Feature/ExampleTest.php:10
      Illuminate\Database\Eloquent\Factories\Factory::create()

I have dumped composer-autoload and all the various caches...

tykus's avatar

@Brainmaniac are you using the Tests\TestCase class in the Pest tests:

// tests/Pest.php
uses(\Tests\TestCase::class)->in('Feature');
Brainmaniac's avatar

@tykus Sorry nvr mind this I will try a little on my own and open a new question if I don't make it and there define my error better. Thanks for helping me this far!

Please or to participate in this conversation.