Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Agoi's avatar
Level 1

PHP UNIT INSERTION ERROR

I am trying out testing my laravel application with the assertion:

class CommentTest extends TestCase
{
    use WithFaker, RefreshDatabase;

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

        $attributes = [
            'body' => $this->faker->name,
        ];

        $this->post('/api/comment/store', $attributes);

        $this->assertDatabaseHas('comments', $attributes);
    }
}

Below is my route definition:

        Route::prefix('comment')->group(function () {
            Route::post('store', ['as' => 'comment.store', 'uses' => 'CommentController@store']);
        });

And my Controller below:

    /**
     * Handle the process of creating a comment
     *
     * @return
     */
    public function store(CommentRequest $request)
    {
        return \App\Comment::create([
            'body' => $request->body
        ]);
    }

I still endpoint having the error below:

1) Tests\Feature\CommentTest::a_user_can_create_a_comment
Failed asserting that a row in the table [comments] matches the attributes {
    "body": "Prof. Llewellyn Cummerata IV"
}.

The table is empty.

Not sure what I am doing wrong but each time I tried to write a test, something just comes up that pushes me away from it.

Ofcouse my phpunit.xml has:

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

Please, what can I be doing wrong here?

0 likes
5 replies
Nakov's avatar

And does the creation works using a traditional form, and it doesn't just for the test?

Did you maybe forgot to add body in the $fillable array within your Comment model?

Agoi's avatar
Level 1

@nakov.

Yes it works when I test from POSTMAN and my model has

    /**
     * Attr that can be mass assigned
     *
     * @var []
     */
    protected $fillable = [
        'body'
    ];
Nakov's avatar

@agoi and you are reaching the controller code for sure? Tried this:

public function store(CommentRequest $request)
{
    dd($request->all());
    return \App\Comment::create([
       'body' => $request->body
   ]);
}
mstrauss's avatar

Hi @agoi

A couple of things to that may be going on here.

It looks like you may have form request validation set up for this request, i.e. store(CommentRequest $request). If that is the case, can you share the CommentRequest form request rules?

Next, I just want to make sure that your route is defined in the routes/api.php file and not the routes/web.php file. Since you said it works with POSTMAN, I assume this is the case.

Finally, and this is just convention not really a potential code-breaking issue, but your test is named a_user_can_create_a_comment but there is no user included in the test, i.e. actingAs($user). Perhaps consider renaming the test to a_comment_can_be_created. Seems like a small, superfluous detail, but when you have hundreds, potentially thousands, of tests, those little details can save you substantial amounts of time over the life of a project.

Agoi's avatar
Level 1

@mstrauss @nakov thanks so much for the heads up.

I eventually got it sorted, the issue was that the route actually requires authentication which I was missing in the test.

Please or to participate in this conversation.