vendredi 23 mars 2018

In memory database for unit testing in laravel 5.5

I intend to use in memory database for unit testing in laravel... I added this lines to phpunit.xml,

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

And this is my BookTest.php file inside \tests\Feature\BookTest.php directory,

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
//use Illuminate\Foundation\Testing\DatabaseMigrations;

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class BookTest extends TestCase
{
    use DatabaseMigrations;

    public function test_books_can_be_created()
    {        
        $user = factory(\App\User::class)->create();

        $book = $user->books()->create([
            'name' => 'The hobbit',
            'price' => 10
        ]);

        $found_book = $book->find(1);

        $this->assertEquals($found_book->name, 'The hobbit');
        $this->assertEquals($found_book->price, 10);


    }
}

But when we tried to add vendor\bin\phpunit I get,

1) Tests\Feature\BookTest::test_books_can_be_created Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: books (SQL: insert into "books" ("name", "price", "user_id", "updated_at", "created_at") values (The hobbit, 10, 1, 2018-03-23 08:52:26, 2018-03-23 08:52:26))

it would be great help if you can help me on how to use in memory database in laravel unit testing.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire