I am trying to run CI tests with Laravel. The tests always fail, however, as the database doesn't appear to be rolled back after each test. This leads to errors such as the following:
Illuminate\Database\QueryException: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'user_roles' already exists (SQL: create table `user_roles` (`id` int unsigned not null auto_increment primary key, `user_id` int unsigned not null, `role_id` int unsigned not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
I tried some code I found on the internet, which uses the DatabaseMigrations trait on the test class, which works fine for the first test, but doesn't work for the rest of them:
/**
* Set up the environment for testing.
*/
public function setUp()
{
parent::setUp();
$this->runDatabaseMigrations();
// Create an example user
$this->user = new User();
$this->user->name = 'Test User';
$this->user->email = '[email protected]';
$this->user->password = '';
$this->user->save();
}
And even tried adding a migrate:rollback call to the tearDown method of the tests, to no avail:
public function tearDown()
{
parent::tearDown();
$this->artisan('migrate:rollback');
}
Any ideas as to how to fix this?