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