7

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('[email protected]', 'email')
       ->type('lorem85', 'password')
       ->press('Se connecter');
}
2

3 Answers 3

21

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

0
2

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

<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>
2
  • Yes i have exactly this configuration for my phpunit.xml
    – Exarkun
    May 15, 2017 at 14:45
  • 1
    When you run tests, the artisan calls won't throw error if they fail. Try checking if the migration and seeding were successful before the test.
    – Sandeesh
    May 15, 2017 at 14:48
1

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

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
1
  • I use traits in each file like UserTest.php , ExampleTest ect
    – Exarkun
    May 15, 2017 at 13:37

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.