Level 10
Anyone?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello, I am doing an old series (Lets Build a Forum with Laravel & TDD), which I am trying to replicate everything using Vue/Inertia. However, I am having some issues with the testing. Can you help me re-write this test to be compatible with Inertia?
public function an_authenticated_user_may_participate_in_forum_threads()
{
// Given we have an authenticated user (log a user in)
$user = User::factory()->create();
$this->be($user);
// And an existing thread (created by any user)
$thread = Thread::factory()->create();
// When the user adds a reply to the thread
$reply = Reply::factory()->make(['thread_id' => $thread->id, 'user_id' => $user->id]);
// Then their reply should be visible on the page
// HOW TO REWRITE THIS PART FOR INERTIA?
$this->post($thread->path(), $reply->toArray());
// reload the thread, and check that the reply is there
$this->get($thread->path())
->assertSee($reply->owner->name)
->assertInertia(function ($assert) use ($reply) {
$assert->component('Discussion/Show');
$assert->has('replies.data', 1);
$assert->where('replies.data.0.body', $reply->body);
});
}
Currently when I run the test I am getting this error:
Property [replies.data] does not have the expected size.
Failed asserting that actual size 0 matches expected size 1.
So I assume that the $reply->toArray() section doesn't worth with Inertia testing?
I figured it out after hours of messing around. For anyone that is wanting to know how to write tests for Inertia you can refer to this:
<?php
namespace Tests\Feature\Discussion;
use App\Models\Reply;
use App\Models\Thread;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Inertia\Testing\AssertableInertia;
use Tests\TestCase;
class ParticipateInForumTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function an_authenticated_user_may_participate_in_forum_threads()
{
// Given we have an authenticated user (log a user in)
$user = User::factory()->create();
$this->be($user);
// And an existing thread (created by any user)
$thread = Thread::factory()->create();
// When the user adds a reply to the thread
$reply = Reply::factory()->make(['thread_id' => $thread->id, 'user_id' => $user->id]);
// Post the reply to the thread
$this->post(route('discuss.replies.store', $thread->slug), $reply->toArray());
// Then their reply should be visible on the page
$this->get(route('discuss.show', $thread->slug))
->assertSee($reply->owner->name)
->assertInertia(function (AssertableInertia $page) use ($reply) {
$page->component('Discussion/Show')
->has('replies.data', 1)
->where('replies.data.0.body', $reply->body);
});
}
}
Please or to participate in this conversation.