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

问题描述 投票:0回答:1

我正在使用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第一次写测试用例。请指导我完成这个过程。

laravel unit-testing lumen
1个回答
4
投票

如果你使用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 DocumentationLumen Testing Documentation

© www.soinside.com 2019 - 2024. All rights reserved.