rudexpunx
2 years ago
1796
13
Testing

RefreshDatabase trait doesn't refresh database

Posted 2 years ago by rudexpunx

From the doc.: ###Resetting The Database After Each Test

It is often useful to reset your database after each test so that data from a previous test does not interfere with subsequent tests. The RefreshDatabase trait takes the most optimal approach to migrating your test database depending on if you are using an in-memory database or a traditional database. Simply use the trait on your test class and everything will be handled for you.

However, it doesn't work for me.

For example, I am creating (only) 3 new posts in an XYth test, however, their IDs are 25, 26, 27.

If I tweak the refreshTestDatabase() method on RefreshDatabase trait by commenting the condition as you can see below, everything works fine and all my tests do pass, even if it takes more time to run the tests...

/**
     * Refresh a conventional test database.
     *
     * @return void
     */
    protected function refreshTestDatabase()
    {
        // if (! RefreshDatabaseState::$migrated) {
            $this->artisan('migrate:fresh');

            $this->app[Kernel::class]->setArtisan(null);

            RefreshDatabaseState::$migrated = true;
        // }

        $this->beginDatabaseTransaction();
    }

Laravel 5.5

Please sign in or create an account to participate in this conversation.