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

时间:2019-02-14 10:29:37

标签: laravel unit-testing lumen

我正在使用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);
    }
}

我第一次在流明扭扭测试用例。请指导我完成此过程。

2 个答案:

答案 0 :(得分:3)

如果使用流明的Laravel\Lumen\Testing\TestCase和Laravel的默认Illuminate\Foundation\Testing\TestCase,则断言的某些方式会有所不同。

如果要为Illuminate\Foundation\Testing\TestCase声明assertStatus:

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 DocumentationLumen Testing Documentation

答案 1 :(得分:0)

如果您想要更像Laravel的测试服,则可以使用以下软件包: https://packagist.org/packages/albertcht/lumen-testing

然后您可以使用Laravel之类的断言

相关问题