Laravel测试,没有这样的表,sqlite

Kao*_*Kao 6 php phpunit laravel

我在OS X上使用MAMP运行Laravel,我正在尝试使用PHPUnit和sqlite进行一些单元测试,但是当我运行测试时,我得到了错误

一般错误:1没有这样的表:用户

我已经尝试运行工匠,手动迁移数据库,使用--env = testing,运行正常,但我仍然得到错误.我甚至调用Artisan::call('migrate');了SetUp方法.

/app/config/testing/database.php

return [
    'default' => 'sqlite',

    'connections' => [
        'sqlite' => [
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => ''
        ],
    ]
];
Run Code Online (Sandbox Code Playgroud)

编辑: 迁移文件:

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');

});
Run Code Online (Sandbox Code Playgroud)

Ale*_*ski 7

SQLite不支持以下ENUM类型:http://www.sqlite.org/datatype3.html

这就是为什么没有创建用户表的原因.您必须在经典MySQL表上运行测试或删除所有ENUM类型.


mar*_*nuy 6

在运行测试之前运行迁移不起作用,因为正如您的配置所示,您的数据库位于内存中因此每次运行新测试时,它都会为每个测试会话运行迁移,并在完成时完全销毁所有测试数据。

要使用内存数据库,请在每次运行测试时运行迁移,使用以下内容从 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();
  }
}
Run Code Online (Sandbox Code Playgroud)