【问题标题】:Laravel PhpUnit No such tableLaravel PhpUnit 没有这样的表
【发布时间】:2017-10-14 07:39:59
【问题描述】:

我尝试了很多东西,但我被这个问题困住了。 我尝试对我的应用程序进行测试(使用 Laravel5.3)。 我的开发数据库是 Mysql ,但我想用 sqlite"memory" 数据库进行测试。

每次我尝试启动测试时都会出现以下错误: 一般错误:1没有这样的表:groupe_user

它似乎不会迁移 sqlite 数据库中的表。 我看不出我做错了什么。

我把我的 testCase 文件和迁移放在这里,如果有人可以帮助我,那就太好了。

TestCase.php:

<?php

abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
 * The base URL to use while testing the application.
 *
 * @var string
 */
protected $baseUrl = 'http://localhost';

/**
 * Creates the application.
 *
 * @return \Illuminate\Foundation\Application
 */
public function createApplication()
{
      $unitTesting = true;
      $testEnvironment = 'testing';

    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();

    return $app;
}

public function setUp()
{
     parent::setUp();
     $this->createApplication();
     $this->prepareForTests();
}

 private function prepareForTests()
 {
     Artisan::call('migrate');
     Artisan::call('db:seed');
 }

 public function tearDown()
 {
 parent::tearDown();
  }

}

以及带有该数据透视表的迁移文件:

class CreateGroupesTable extends Migration
{

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('groupes', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 100);
        $table->timestamps();
    });

//Création de la table pivot groupe_user avec les cléfs étrangères
Schema::create('groupe_user', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned()->index()->nullable();
    $table->integer('groupe_id')->unsigned()->index()->nullable();
    $table->foreign('user_id')->references('id')->on('users');
    $table->foreign('groupe_id')->references('id')->on('groupes');
    $table->timestamps();
});
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('groupes');

}

}

感谢收看。

编辑: 我的 AuthTest.php 的开始

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\User;

class AuthTest extends TestCase
{
use DatabaseMigrations;

public function testAuthLogin()
{
    $user = factory(App\User::class)->create();

//Test du login
    $this->visit('/login')
       ->see('Se Connecter')
       ->type('lorem@gmail.com', 'email')
       ->type('lorem85', 'password')
       ->press('Se connecter');
}

【问题讨论】:

标签: laravel sqlite phpunit


【解决方案1】:

对于 Laravel 5.3 或更早的版本(但在 Laravel 5 中),根据the official reference of Ver.5.3,您的测试用例文件中似乎需要以下内容(我没有尝试过,但从 Ver. 5.6;见下文)。我的猜测是类定义中的use DatabaseMigrations; 是必不可少的。

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase {
    use DatabaseMigrations;
    // Your test statements.
}

对于 Laravel 5.4 及更高版本(请参阅 the official reference of Ver.5.6),以下适用于我,在 phpunit.xml 文件中将 DB_DATABASE 设置为“:memory:”:

use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase {
    use RefreshDatabase;
    // Your test statements.
}

无论哪种方式,迁移都必须从空数据库正常运行,才能为您的应用程序创建空表(或者至少对于您的单元测试脚本来说足够好)。

【讨论】:

    【解决方案2】:

    您是否在 phpunit.xml 文件中设置了 sqlite 数据库信息?

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
    </php>
    

    【讨论】:

    • 是的,我的 phpunit.xml 正好有这个配置
    • 运行测试时,artisan 调用在失败时不会抛出错误。尝试在测试前检查迁移和播种是否成功。
    【解决方案3】:

    如果你想测试数据库事务,你必须使用Traits in your testcase

    use Illuminate\Foundation\Testing\DatabaseMigrations;
    use Illuminate\Foundation\Testing\DatabaseTransactions;
    

    【讨论】:

    • 我在每个文件中使用特征,例如 UserTest.php , ExampleTest 等
    猜你喜欢
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2018-07-26
    • 2016-08-23
    • 2017-02-19
    • 2012-09-28
    相关资源
    最近更新 更多