vendredi 29 mars 2019

"Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: users" on using SQLite :memory: for phpunit testing in Laravel 5

I'm using SQLite :memory: for only phpunit testing in Laravel whereas all my actual database data (all tables) are in MySQL which I use for development and are working fine when I manually test it. I'm not familiar with how SQLite :memory: works and if I have to separately create database and tables for it when I'm using it to write tests? or does :memory: does that itself?

I expect my phpunit tests to pass but I get

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: users

Here is my phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
    </php>
</phpunit>

Here is the test I'm running:


   /** @test */
   public function user_can_view_questions()
   {
       $question = factory('App\Question')->create();
       // not writing the rest of code as this returns as an error

   }

and here is the factory if it is needed:

<?php

use App\User;
use Illuminate\Support\Str;
use Faker\Generator as Faker;
use App\Question;
use App\Answer;


$factory->define(User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
        'remember_token' => Str::random(10),
    ];
});

$factory->define(Question::class, function (Faker $faker) {
    return [
        'user_id' => function(){
            return factory('App\User')->create()->id;
        },
        'subject_id' => $faker->randomDigit,
        'class_id' => $faker->randomDigit,
        'chapter_id' => $faker->randomDigit,
        'topic_id' => $faker->randomDigit,
        'qnop' => $faker->sentence,
        'qtitle' => $faker->sentence,
        'qdetails' => $faker->sentence
     ];
});

$factory->define(Answer::class, function (Faker $faker) {
    return [
        'user_id' => function(){
            return factory('App\User')->create()->id;
        },
        'question_id' => function(){
            return factory('App\Question')->create()->id;
        },
        'rating' => $faker->numberBetween($min = 0, $max = 5),
        'ans' => $faker->sentence
     ];
});




via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire