Laravel PhpUnit 没有这样的表

Exa*_*kun 5 sqlite phpunit laravel

我尝试了很多东西,但我被这个问题困住了。我尝试对我的应用程序进行测试(使用 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();
  }
Run Code Online (Sandbox Code Playgroud)

}

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

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

}
Run Code Online (Sandbox Code Playgroud)

}

谢谢观看。

编辑:我的 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');
}
Run Code Online (Sandbox Code Playgroud)

Mas*_*ano 16

对于 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.
}
Run Code Online (Sandbox Code Playgroud)

对于 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.
}
Run Code Online (Sandbox Code Playgroud)

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


小智 1

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

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
Run Code Online (Sandbox Code Playgroud)