My database is empty after running my laravel tests
Asked Answered
B

2

5

Each time I run my tests with

php artisan test

after when I want to see the inserted data in my database, the table is totally empty.

Here is my test

class ExampleTest extends TestCase
{

    use RefreshDatabase;

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $ad = new Ad();
        $ad->name = 'test';
        $ad->price = 100000;
        $ad->save();
        $this->assertTrue($ad->id > 0);
        var_dump($ad->id);
    }

}

The test verifies if the database has well generated an id, and the result is correct each time.

From my point of view, the trait RefreshDatabase refreshes the database BEFORE each tests not after so .. how in the world is this possible?

NB: I am sure that I am checking the correct database because if I remove use RefreshDatabase I can see the records.

Precision:

If instead of RefreshDatabase I use DatabaseTransactions I can see the records after running tests. I can't get my head around it...

Belleslettres answered 12/2, 2021 at 12:12 Comment(5)
It is is expected from RefreshDatabase to clear the DB so tests don't affect eachother.Incipient
@Incipient yes before, I have only one test method in my entire suite, so I must be able to see the records in the database ..Belleslettres
Your understanding of when the database is refreshed is incorrect, the database is cleared after each test.Secant
Oh well, you are right. I'm feeling a bit uncomfortable right now .. but this behavior is really strange, it is often really useful to be able to read to record in the database after a tests failed for example ..Belleslettres
The result of your failed assertions should give you a good idea as to what is wrong. Might need to make your tests/assertions more expresive.Secant
B
6

I am assuming you are using RefreshDatabase already.

The first thing to check is your phpunit.xml file and its DB settings. It should be an in-memory database.

The second thing to check you are not caching the configs on the dev environment.

First run php artisan config:clear only once. Then run your tests.

Note: do not cache the config for local development.

Balch answered 10/11, 2021 at 23:51 Comment(0)
C
2

Add this to your phpunit.xml file

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>

Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory>tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory>tests/Feature</directory>
        </testsuite>
    </testsuites>
    <source>
        <include>
            <directory>app</directory>
        </include>
    </source>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>
Carder answered 30/9, 2023 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.