b1narylife's avatar

"The table is empty :-("

No sure if I should post it here or create a discussion so apologies if this isn't the right place to do it. Anyway,

I am going through build-a-laravel-app-with-tdd and I have followed along as one does however when I run phpunit, I get the error (pasted below my code). I am not sure why this is happening and would appreciate some help. My phpunit.xml has been updated so that I am using sqlite and memory for testing purposes and not my actual local database.

If I switch the test to "assertDatabaseMissing" it passes absolutely fine, so it seems like it is not inserting rows that it can read from?? idk!

ProjectsTest.php


<?php

namespace Tests\Feature;

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

class ProjectsTest extends TestCase
{
    use WithFaker, RefreshDatabase;

    /** @test */
    public function a_user_can_create_a_project()
    {


        $this->withoutExceptionHandling();

        $attributes = [
            'title' => $this->faker->sentence,
            'description' => $this->faker->paragraph
        ];

//        $attributes = [
//            'title' => 'This title is hardcoded...',
//            'description' => 'Figure out how to make faker work in 6.0'
//        ];

        $this->post('/projects', $attributes);

        $this->assertDatabaseHas('projects', $attributes);

//        $this->get('/projects')->assertSee($attributes['title']);

    }
}

CLI Output


There was 1 failure:

1) Tests\Feature\ProjectsTest::a_user_can_create_a_project
Failed asserting that a row in the table [projects] matches the attributes {
    "title": "Qui quo dolorem in numquam excepturi mollitia.",
    "description": "Omnis aut fuga sit atque culpa exercitationem. Vitae commodi aliquid voluptatem ipsam. Fuga quam et in assumenda dolorem corporis qui."
}.

The table is empty.

/Users/damienk.sedgwick/Sites/birdboard/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:24
/Users/damienk.sedgwick/Sites/birdboard/tests/Feature/ProjectsTest.php:32

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

Thanks in advance for your help :-)

0 likes
12 replies
DavidLi's avatar

You need to log in as a user first, try this in your test method:

$user = factory(User::class)->create();
$this->be($user);

Doesn't your post have a user_id attirubute there? If so, pass in the user_id too.

b1narylife's avatar

Hmm I am simply following the tutorial and there is no user / id in ProjectsTest.php , however I have included your snippet for the user and I get the following repsonse:


1) Tests\Feature\ProjectsTest::a_user_can_create_a_project
Error: Class 'Tests\Feature\User' not found

/Users/damienk.sedgwick/Sites/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:291
/Users/damienk.sedgwick/Sites/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:122
/Users/damienk.sedgwick/Sites/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:300
/Users/damienk.sedgwick/Sites/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:219
/Users/damienk.sedgwick/Sites/birdboard/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:178
/Users/damienk.sedgwick/Sites/birdboard/tests/Feature/ProjectsTest.php:20


mvd's avatar

@b1narylife

Are you sure the columns title and description exists or are there more required fields?

If a remove $this->withoutExceptionHandling() and a have a wrong column name / missing a required field I get the same error/failere.

1 like
b1narylife's avatar

Here is my create_projects_table:


<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProjectsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('projects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('projects');
    }
}


This is copied directly from Jeff in the tuts. I am sure the columns exist but I could be missing something so I will go back through the video up until this point to check!

mvd's avatar

@b1narylife are you sure $this->withoutExceptionHandling(); is in your test and not disabled?

Can you give us the store method (where you save a new project) code from your projects controller?

b1narylife's avatar

** Update -> I am going back through the first two episodes and will return if I encounter the same problem. I am going to use the same version of laravel this time too instead of 6.0 although I doubt that was the issue.

DavidLi's avatar

Oh, man. You need to import the App\User on the top, you do know that, right?

dzalev's avatar

in your Project model just put

protected $guarded = [];

Maxlaravel's avatar

You need to persist the data in the database:

in ProjectController - store method:

$attributes=request(['title', 'description']);

Project::create($attributes);

off course you need to validate the data too

wowzaaa's avatar

Hi,

First of all try to use for this method "public function a_user_can_create_a_project()"  the word "test" instead of the word "a" like this : 
  • "test_user_can_create_a_project" , because you can find errors because of this. Second of all the error "The table is empty" says that the table "projects" does not exist inside your database or can not find it for some kind of reason. You can find over here, https://laravel.com/docs/7.x/database-testing, what assertDatabaseHas does.

Hope you can find the problem or already resolved it!

jmourell's avatar

I am having the same issue, I am using Laravel 8 when I refactor to use controllers it pops up. Is this a difference in how data is persisted in laravel 8, anyone else have the same problem and fixed it?

jmourell's avatar

nevermind found the issue I had misconfigured my routes to swapping index and store Route::get('/projects', [ProjectsController::class, 'store']); Route::post('/projects', [ProjectsController::class, 'index']); :)

Please or to participate in this conversation.