【发布时间】:2019-07-08 08:27:02
【问题描述】:
我正在使用 Lumen 构建 api,并且还想为此编写单元测试用例。但我面临的问题不是单一的断言方法正在工作。像assertStatus()、assertNotFound()、assertJson() 等。它们都给出了错误,因为 Call to undefined method 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 中编写测试用例。请指导我完成此过程。
【问题讨论】:
-
可以在顶部加
use Illuminate\Foundation\Testing\TestCase吗? -
添加到顶部后显示“Illuminate\Foundation\Testing\TestCase”类未找到。
-
TestCase.php 文件已经存在于同一目录中。所以我认为没有必要通过使用来包含它。
-
你能检查一下
TestCase类是从什么扩展而来的吗?要使这项工作TestCase应该扩展Illuminate\Foundation\Testing\TestCase和$response然后必须是Illuminate\Foundation\Testing\TestResponse -
当然。它是从下面的这个类扩展而来的。
<?php abstract class TestCase extends Laravel\Lumen\Testing\TestCase { /** * Creates the application. * * @return \Laravel\Lumen\Application */ public function createApplication() { return require __DIR__.'/../bootstrap/app.php'; } }
标签: laravel unit-testing lumen