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?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
Please or to participate in this conversation.