-
After upgrading to Laravel 11 my tests are failing with a no such table error for tables in a secondary DB connection. I am using in memory DBs with SQLITE for my tests. Also, tests work fine when ran isolated, they only fail when running them all. So I think it is a problem related to the RefreshDatabase trait. I read in the release notes for Laravel 11 that tests using memory dbs are faster now. Maybe this problem is related to this change? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Have you found a cause of this? Facing the same issue |
Beta Was this translation helpful? Give feedback.
-
We are searching for fix too, everything is working perfect when I remove ":memory:" but when I put it back the tables are missing |
Beta Was this translation helpful? Give feedback.
-
I found the reason why. For my some of the tests we were using sqlite and for some mysql and with laravel 11 you can not change databases as RefreshDatabase trait does not account for that. So to be able to test on both databases I have written these traits: namespace Tests\Traits;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\RefreshDatabase as BaseRefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
trait RefreshDatabase
{
use BaseRefreshDatabase;
protected function beforeRefreshingDatabase()
{
DB::setDefaultConnection('sqlite-memory');
config(['telescope.storage.database.connection' => 'sqlite-memory']);
config(['queue.batching.database' => 'sqlite-memory']);
config(['job-status.database_connection' => 'sqlite-memory']);
}
/**
* Refresh a conventional test database.
*
* @return void
*/
protected function refreshTestDatabase()
{
$connection = DB::getDefaultConnection();
$migrated = TestCase::$migrated[$connection] ?? false;
if (! $migrated) {
$this->artisan('migrate:fresh', $this->migrateFreshUsing());
$this->app[Kernel::class]->setArtisan(null);
RefreshDatabaseState::$migrated = true;
TestCase::$migrated[$connection] = true;
}
$this->beginDatabaseTransaction();
}
} And for mysql I use this: namespace Tests\Traits;
trait RefreshMysqlDatabase
{
use RefreshDatabase;
protected function beforeRefreshingDatabase()
{
}
} You also need to update your databases config and add explicit |
Beta Was this translation helpful? Give feedback.
-
Sorry for the delay, I haven't found an error yet. All my dbs for testing are SQLite in memory, so I'm not sure this will work for me. Anyways I'll give it a try, and let you know Thanks! |
Beta Was this translation helpful? Give feedback.
-
I finally was able to find the fix. In order for the new RefreshDatabase Trait to work with multiple database connections, you have to specify the names of the databases in your Test class using the Example:
|
Beta Was this translation helpful? Give feedback.
I finally was able to find the fix.
In order for the new RefreshDatabase Trait to work with multiple database connections, you have to specify the names of the databases in your Test class using the
connectionsToTransact
property.Example: