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?
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With