Laravel PhpUnit 没有这样的表

问题描述 投票:0回答:3

我尝试了很多事情,但我陷入了这个问题。 我尝试对我的应用程序进行测试(使用 Laravel5.3)。 我的开发数据库是 Mysql ,但我想使用 sqlite“内存”数据库进行测试。

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

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

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

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('[email protected]', 'email')
       ->type('lorem85', 'password')
       ->press('Se connecter');
}
laravel sqlite phpunit
3个回答
22
投票

对于 Laravel 5.3 或更早版本(但在 Laravel 5 中),根据 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 及更高版本(请参阅 Ver.5.6 的官方参考),以下内容适用于我,并将

DB_DATABASE
文件中的
:memory:
设置为“
phpunit.xml
”:

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>

1
投票

如果您想测试数据库事务,则必须使用测试用例中的特征

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
© www.soinside.com 2019 - 2024. All rights reserved.