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

CookieMonster's avatar

php artisan test not passing a test case

I am new with TDD and tried to implement few test cases in my app.

ReadThreadTest:


class ReadThreadsTest extends TestCase
{
    use DatabaseMigrations;


    public function setUp():void{
        parent::setUp();
        $this->thread = factory('App\Thread')->create();
    }


    /**
     * A basic test example.
     * @test
     * @return void
     */
    public function a_user_can_view_all_threads()
    {
        
        $thread = factory('App\Thread')->create(); 

        $response = $this->get('/threads');
        $response->assertSee($thread->title);

      
    }

     /**
     * A basic test example.
     * @test
     * @return void
     */
    public function a_user_can_read_a_single_thread()
    {
        
        $thread = factory('App\Thread')->create(); 
        $response = $this->get('/threads/' .$this->thread->id); 
        $response->assertSee($this->thread->tiitle);
    }
      

    public function a_user_can_read_replies_associated_with_a_thread(){
        //Given a thread, a thread include replies
        //And that thread include replies,when we visit a thread page, we should see replies
        
        $reply = factory('App\Reply')->create(['thread_id' => $this->thread->id]);
        
        $this->get('/threads/'.$this->thread->id)
        ->assertSee($reply->body);

    }
}

When I run php artisan test, it shows all the test functions passed except for the last one (a user can read replies associated with a thread). It doesn't show any error so I am not sure why it is not passed. Can anyone give any pointers?

0 likes
6 replies
haztakki's avatar

So it knows to include the test when you run all of them using the command without the flag, else you'll need to specify the test. The annotation basically says, 'hey, include me in the test' when running all.

Tray2's avatar

Also you are refering to $this->thread->id in the last test and that does not exist as a property in your class.

Add it above your setup method.

Tray2's avatar

It doesn't matter if it's public, protected or private.

Please or to participate in this conversation.