Notice that we call the factory’s make method instead of create. What actually adds the reply to the thread page is the post request, where we send through the attributes for the reply.
How does reply get added to correct thread in this test?
I'm currently revising the Lesson 4 of building a forum with TDD approach and wondering why the following test works [ Ref: https://github.com/laracasts/Lets-Build-a-Forum-in-Laravel/blob/76a05a038ae9a846df9b11ab1fcff48376ce74cf/tests/Feature/ParticipateInThreadsTest.php ]
In the following test, we create a thread and then create a reply and assume that the reply gets added to the correct thread.
Why does it work without having to write:
$reply = factory('App\Reply')->create(['thread_id' => $thread->id]);
I think we need to specify the thread_id explicitly for the system to understand which thread to add the reply to.
Yet, everything works fine. I'm not sure why. Can someone spare a moment to explain?
/** @test */
function an_authenticated_user_may_participate_in_forum_threads()
{
$this->be($user = factory('App\User')->create());
$thread = factory('App\Thread')->create();
$reply = factory('App\Reply')->make();
$this->post($thread->path() . '/replies', $reply->toArray());
$this->get($thread->path())->assertSee($reply->body);
}
Please or to participate in this conversation.