【发布时间】: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.com/docs/5.3/database-testing 将
use DatabaseMigrations;添加到您的测试中 -
我要编辑我的帖子只是为了显示我的一个文件的开头。