首页 > 解决方案 > 调用未定义的方法 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 编写测试用例。请指导我完成这个过程。

标签: laravelunit-testinglumen

解决方案


如果您使用 LumenLaravel\Lumen\Testing\TestCase 与 Laravel 的 default ,一些断言方法会有所不同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 测试文档流明测试文档


推荐阅读