调用未定义的方法 ExampleTest::assertStatus()

Call to undefined method ExampleTest::assertStatus()

我正在使用 Lumen api 构建并且还想为此编写单元测试用例。但我面临的问题不是单一的断言方法在起作用。像 assertStatus()assertNotFound()assertJson() 等。所有这些都给出错误 调用未定义的方法 ExampleTest::assertMethod()。下面是我的 ExampleTest 文件。

<?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);
    }
}

我第一次在 Lumen 编写测试用例。请指导我完成这个过程。

如果您使用 Lumen 的 Laravel\Lumen\Testing\TestCase 与 Laravel 的默认 Illuminate\Foundation\Testing\TestCase.

,则断言的一些方法会有所不同

如果您想为 Illuminate\Foundation\Testing\TestCase 断言状态:

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

        $response->assertStatus(200);
    }

Laravel\Lumen\Testing\TestCase 相同:

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

        $this->assertEquals(200, $this->response->status());
    }

Laravel Testing Documentation and Lumen Testing Documentation

如果你想要一个更像 Laravel 的测试服,你可以使用这个包: https://packagist.org/packages/albertcht/lumen-testing

然后你可以使用像Laravel

这样的断言