It looks like the issue you're encountering is related to the database transactions and the LazilyRefreshDatabase trait. When using Pest with Laravel, the LazilyRefreshDatabase trait can cause the database to be refreshed between tests, which might be why you're seeing your tables being emptied.
To address this, you can try the following steps:
-
Remove the
LazilyRefreshDatabasetrait from yourTestCaseclass if you don't want the database to be refreshed between tests. -
Use
DatabaseTransactionsin your test classes to ensure that each test runs within a database transaction, which will be rolled back after the test completes.
Here's how you can adjust your TestCase and ClientTest classes:
TestCase Class
Remove the LazilyRefreshDatabase trait:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
// Remove LazilyRefreshDatabase trait
}
ClientTest Class
Ensure you are using the DatabaseTransactions trait:
<?php
namespace Tests\Unit;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use App\Models\Client;
class ClientTest extends TestCase
{
use DatabaseTransactions;
/**
* Test info
*
* @return void
*/
public function testname()
{
// Your test code here
}
}
Example of a Pest Test
If you want to use Pest with database transactions, you can do something like this:
<?php
use Illuminate\Foundation\Testing\DatabaseTransactions;
beforeEach(function () {
// This will ensure each test runs within a transaction
$this->beginDatabaseTransaction();
});
test('example', function () {
// Your test code here
$this->assertTrue(true);
});
By ensuring that your tests run within transactions and removing the LazilyRefreshDatabase trait, you should be able to prevent the database from being emptied between tests. This approach will help maintain the state of your database as expected during testing.