Laravel 测试,没有这个表,sqlite

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21926489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:04:20  来源:igfitidea点击:

Laravel testing, no such table, sqlite

phplaravelphpunit

提问by Kao

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

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

General error: 1 no such table: users

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

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.

我试过运行工匠,手动迁移数据库,使用 --env=testing,运行良好,但我仍然收到错误。我什至调用Artisan::call('migrate');了 SetUp 方法。

/app/config/testing/database.php

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

});

回答by Alexandre Butynski

SQLite doesn't support the ENUMtype : http://www.sqlite.org/datatype3.html

SQLite 不支持ENUM类型: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 ENUMtypes.

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

回答by marcanuy

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.

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

To use the memorydatabase, run your migrations each time you run your tests, extend your classes from TestCase with something like:

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