Laravel PhpUnit 没有这样的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43980861/
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 15:57:57  来源:igfitidea点击:

Laravel PhpUnit No such table

laravelsqlitephpunit

提问by Exarkun

I had try lot of things but i am stuck on this problem. I try to make test on my application (working with Laravel5.3). My DB for developement is Mysql , but i want test with the sqlite"memory" database.

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

Each time i try to launch a test i have this error: General error: 1 no such table: groupe_user

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

Its seem to don't migrate tables in the sqlite database. I don't see what i am doing wrong.

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

I put here my testCase file and the migrations if someone can help me , it would be great.

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

The TestCase.php :

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();
  }

}

}

And the migration file with that pivot Table :

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

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

}

}

}

Thanks for watching.

谢谢观看。

EDIT: The begining of my AuthTest.php

编辑:我的 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');
}

回答by Masa Sakano

For the versions of Laravel 5.3 or earlier (but in Laravel 5), according to the official reference of Ver.5.3, it seems the following is necessary in your test-case file (I have not tried, but guessed from the behaviour in Ver.5.6; see below). My guess is use DatabaseMigrations;in the class definition is essential.

对于 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.
}

For Laravel 5.4 and later (see the official reference of Ver.5.6), the following works for me with the DB_DATABASEset to be ":memory:" in the phpunit.xmlfile:

对于 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.
}

Either way, the migration has to work well from the empty database to create the empty tables for your application (or at least good enough for your unit-test scripts).

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

回答by Sandeesh

Have you setup the sqlite database info in the phpunit.xml file?

您是否在 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>

回答by trommelaap

You have to use the Traits in your testcaseif you want to test database transactions.

如果要测试数据库事务,则必须在测试用例中使用Traits

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