mercredi 25 septembre 2019

Laravel's assertDatabaseHas during testing always returns "The table is empty."

Laravel during testing assertDatabaseHas always returns "The table is empty."

My environmental:

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

My test is:

    use RefreshDatabase;

    /**
     * test
     */
    public function test_create_new_dashboard()
    {
        $this->withoutExceptionHandling();

        $this->actingAs($this->user);

        $dashboard = factory(DashboardsModel::class)->create([
            'title' => 'some',
            'owner_id' => $this->user->id
        ]);

        $this->assertDatabaseHas($dashboard->getTable(), [
            'title' => 'some'
        ]);
    }

But I always receive:

ema@0d3db2c1ce07:/var/www/html$ ./vendor/phpunit/phpunit/phpunit --filter test_create_new_dashboard
PHPUnit 7.5.16 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 137 ms, Memory: 22.00 MB

There was 1 failure:

1) Tests\Feature\DashboardTest::test_create_new_dashboard
Failed asserting that a row in the table [dashboards] matches the attributes {
    "title": "some"
}.

The table is empty.

/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:24
/var/www/html/tests/Feature/DashboardTest.php:47

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

** The user always exists in another table, but not the Dashboard entity. What's wrong? **

Aucun commentaire:

Enregistrer un commentaire