3

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;

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.

3
  • 2
    Do you have a migration that creates that books table? Commented May 31, 2018 at 2:59
  • As I remember not. Do I need to have migrations too?. Really sorry if stupid Commented May 31, 2018 at 8:26
  • Yes you have ‘use databasemigrations’ that runs migrations Commented May 31, 2018 at 8:27

1 Answer 1

3

You should

use RefreshDatabase

so your test should look like this

<?php

    namespace Tests\Feature;

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

    class BookTest extends TestCase
    {
       use RefreshDatabase;

       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);
    }
}

source https://laravel.com/docs/5.5/database-testing#resetting-the-database-after-each-test

Sign up to request clarification or add additional context in comments.

2 Comments

I have the same problem, and this is not the solutions here
What is the problem exactly?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.