Search code examples
laravelunit-testinglumen

Call to undefined method ExampleTest::assertStatus()


I'm using Lumen for api building and also want to write unit test cases for that. But the problem I'm facing is not a single assert method is working. Like assertStatus(), assertNotFound(), assertJson(), etc. All of them giving error as Call to undefined method ExampleTest::assertMethod(). Below is my ExampleTest file.

<?php

use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->get('/');

        $this->assertEquals(
            $this->app->version(), $this->response->getContent()
        );
    }

    /** @test */
    public function testExample2()
    {
        $response = $this->get('/');

        //getting error here
        $response->assertStatus(200);
    }
}

I'm wring test cases for the first time in Lumen. Plese guide me through this process.


Solution

  • Some of the ways to assert are different if you are using Lumen's Laravel\Lumen\Testing\TestCase vs Laravel's default Illuminate\Foundation\Testing\TestCase.

    If you want to assertStatus for Illuminate\Foundation\Testing\TestCase:

    public function testHomePage()
        {
            $response = $this->get('/');
    
            $response->assertStatus(200);
        }
    

    The same for Laravel\Lumen\Testing\TestCase :

    public function testHomePage()
        {
            $response = $this->get('/');
    
            $this->assertEquals(200, $this->response->status());
        }
    

    Laravel Testing Documentation and Lumen Testing Documentation