Laravel PhpUnit No such table

Viewed 13482

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.

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

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

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

The 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

<?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');
}
3 Answers

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.

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_DATABASE set to be ":memory:" in the phpunit.xml file:

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).

Related