Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`RefreshDatabase` drops all tables

In a Laravel/PHPunit test class I added use RefreshDatabase to the class, I understand this should make it so that changes to the database during a test get reverted when the test finishes.

But whenever I run the tests in the class, all tables in the database get dropped, and the tests fail (because the tables don't exist!).

The docs suggest that getting the db to revert after a test is as simple as adding the one line as I did, am I missing something?

like image 447
cb7 Avatar asked Jul 10 '19 15:07

cb7


1 Answers

If you don't want to wipe and rebuild the database with RefreshDatabase, you can simply use the DatabaseTransactions trait. This will roll back any changes made during testing.

<?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    use DatabaseTransactions;

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}
like image 164
matticustard Avatar answered Oct 12 '22 23:10

matticustard