I am running Laravel, on OS X using MAMP, and I am trying to do some unit testing, using PHPUnit, and sqlite, however when I run my tests, I get the error
General error: 1 no such table: users
I've tried running the artisan, to manually migrate the database, using the --env=testing, which runs fine, however I still get the error. I even call Artisan::call('migrate');
in the SetUp method.
/app/config/testing/database.php
return [
'default' => 'sqlite',
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
],
]
];
EDIT: Migration file:
Schema::create('users', function($table) {
$table->increments('id');
$table->string('email', 32)->unique();
$table->string('password', 64);
$table->string('username', 32)->unique();
$table->dateTime('birthdate');
$table->enum('status', array('ACTIVE', 'INACTIVE'));
$table->timestamps();
});
Schema::create('roles', function($table) {
$table->increments('id');
$table->string('name', 32);
});
Schema::create('role_user', function ($table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('role_id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('role_id')->references('id')->on('roles');
});
SQLite doesn't support the ENUM
type : http://www.sqlite.org/datatype3.html
That's why the users table isn't created. You will have to run your tests on a classic MySQL table or remove all the ENUM
types.
Running migrations before running the test doesn't work because, as your config shows, you db is in memory, so every time you run a new test, it runs migrations for each testing session and completely destroys all testing data when it finishes.
To use the memory database, run your migrations each time you run your tests, extend your classes from TestCase with something like:
<?php
class TestCase extends Illuminate\Foundation\Testing\TestCase {
public function createApplication()
{
$unitTesting = true;
$testEnvironment = 'testing';
return require __DIR__.'/../../bootstrap/start.php';
}
public function setUp()
{
parent::setUp();
$this->prepareForTests();
}
private function prepareForTests()
{
Artisan::call('migrate');
Artisan::call('db:seed');
}
public function tearDown()
{
parent::tearDown();
}
}
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