vendredi 25 septembre 2020

setting up test environment laravel

I recently set up a new laravel 8 project and I want to run the tests in memory so I set the testing environment to the following:

  • in phpunit.xml I set db
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
  • Database connection config/database.php
'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],
  • .env.testing
APP_NAME="Laravel 8 Project"
APP_ENV=testing
APP_KEY=base64:kNA1tcEgBCybuoN456B4iRqyvfImfTmdKzqYeYGEJhg=

DB_CONNECTION=sqlite
DB_DATABASE=db.sqlite

TELESCOPE_ENABLED=false
  • in test class:
<?php

namespace Tests\Feature;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class ContactFormComponentTest extends TestCase
{
    use RefreshDatabase;

    /** @test  */
    function create_new_user()
    {
        $user = new User();
        $user->name = 'john doe';
        $user->email = 'john.doe@gmail.com';
        $user->password = 'password';

        $savedUser = $user->save();

        $this->assertTrue($savedUser);
    }
}

I also ran:

php artisan config:cache
php artisan cache:clear

After all of this when I run php artisan test --filter create_new_user the test pass but it clears my real db instead of using :memory:

Any Advice,

Aucun commentaire:

Enregistrer un commentaire