Php Laravel测试,无此表,sqlite
我在OSX上使用MAMP运行Laravel,并尝试使用PHPUnit和sqlite进行一些单元测试,但是当我运行测试时,我发现了错误 一般错误:1没有这样的表:用户 我尝试运行artisan,使用--env=testing手动迁移数据库,运行良好,但仍然出现错误。我甚至调用Php Laravel测试,无此表,sqlite,php,laravel,phpunit,Php,Laravel,Phpunit,我在OSX上使用MAMP运行Laravel,并尝试使用PHPUnit和sqlite进行一些单元测试,但是当我运行测试时,我发现了错误 一般错误:1没有这样的表:用户 我尝试运行artisan,使用--env=testing手动迁移数据库,运行良好,但仍然出现错误。我甚至调用Artisan::call('migrate') /app/config/testing/database.php return [ 'default' => 'sqlite', 'connection
Artisan::call('migrate')设置方法中的代码>
/app/config/testing/database.php
return [
'default' => 'sqlite',
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
],
]
];
编辑:
迁移文件:
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');
});
在运行测试之前运行迁移不起作用,因为如配置所示,您的数据库位于内存中,因此每次运行新测试时,它都会为每个测试会话运行迁移,并在测试完成时完全销毁所有测试数据
要使用内存数据库,请在每次运行测试时运行迁移,并使用以下内容从TestCase扩展类:
<?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();
}
}
SQLite不支持枚举类型:
这就是为什么不创建users表的原因。您必须在一个经典的MySQL表上运行测试,或者删除所有的ENUM
类型。您能向我们展示一下创建表用户的迁移吗?你在应用程序的其余部分使用哪种数据库?MySQL?是的,它是MySQL,我回去工作时会添加迁移文件。在edit中添加迁移解决了我的问题,改为使用“Enum”表。非常感谢。