我尝试了很多东西,但我被这个问题困住了。
我尝试对我的应用程序进行测试(使用 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 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),以下内容适用于我的
DB_DATABASE
在 :memory:
中设置为“phpunit.xml
”文件:use Illuminate\Foundation\Testing\RefreshDatabase;
class ExampleTest extends TestCase {
use RefreshDatabase;
// Your test statements.
}
无论哪种方式,迁移都必须从空数据库运行良好才能为您的应用程序创建空表(或者至少对于您的单元测试脚本来说足够好)。
关于Laravel PhpUnit 没有这样的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43980861/