Summer Sale! All accounts are 50% off this week.

troccoli's avatar

Why is `beforeRefreshingDatabase` not called during Pest testing?

I need to do some checks before refreshing the database, so I thought I could leverage the beforeRefreshingDatabase() method in the RefreshDatabase trait.

My Pest.php file contains

pest()->extend(Tests\TestCase::class)
    ->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
    ->in('Feature', 'Integration');

And my TestCase.php file contains the following

protected function beforeRefreshingDatabase()
{
    Log::info(__METHOD__);
}

However, when I run the tests, the log does not contain the expected line.

Why is that?

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're encountering is likely due to the way Pest handles the RefreshDatabase trait. In Laravel, the beforeRefreshingDatabase method is part of the RefreshDatabase trait, which is typically used in PHPUnit test cases. However, when using Pest, the trait might not be applied in the same way as it is in a standard PHPUnit setup.

Here are a few steps you can take to ensure that beforeRefreshingDatabase is called:

  1. Ensure Proper Trait Usage: Make sure that the RefreshDatabase trait is correctly applied to your test case. In Pest, you might need to explicitly use the trait in your test case class.

  2. Check Pest Configuration: Verify that your Pest configuration is correctly set up to use the TestCase class where the RefreshDatabase trait is applied.

  3. Override the Method in the Correct Class: Ensure that the beforeRefreshingDatabase method is overridden in the class that is actually using the RefreshDatabase trait. If you're using Pest, you might need to create a custom test case class that extends from Illuminate\Foundation\Testing\TestCase and use this class in your Pest tests.

Here's an example of how you might set this up:

// CustomTestCase.php
namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class CustomTestCase extends BaseTestCase
{
    use RefreshDatabase;

    protected function beforeRefreshingDatabase()
    {
        Log::info(__METHOD__);
    }
}

Then, in your Pest.php file, ensure you're using this custom test case:

uses(Tests\CustomTestCase::class)->in('Feature', 'Integration');

By ensuring that the beforeRefreshingDatabase method is in the correct context and that your Pest tests are using the right test case class, you should be able to see the expected log output.

troccoli's avatar

Hmm, for once @laryai was useful.

If I add the RefreshDatabase trait directly in TestCase then it works.

abstract class TestCase extends BaseTestCase
{
    use RefreshDatabase;
    protected function beforeRefreshingDatabase()
    {
        Log::info(__METHOD__);
    }
}

pest()->extend(Tests\TestCase::class)
    ->in('Feature', 'Integration');

But I don't understand why. I mean, the database was refreshed, so the trait's refreshDatabase() method was indeed called.

Please or to participate in this conversation.