Lumen 5.5单元测试帖json

时间:2017-09-18 22:41:03

标签: php unit-testing testing lumen

学习Lumen v5.5 php框架并且遇到了创建我的第一个php单元测试的障碍。如果我设置URL并选择POST方法,并且在正文中选择“raw”并将类型设置为“application / json”,则应用程序使用Postman。所以我们已经知道帖子的工作原理,只是想看一下测试。

这是我认为工作单位测试应该是最接近的事情:

public function testPostJson() {
    $the_json    = '{"client_guid": "C00A0EA5-3F64-01EA-C4B6-159EA145AB3B"}';
    $the_headers = [ 'CONTENT_TYPE' => 'application/json' ];
    $this->call(
        'POST',
        '/getClientNotes',
        [],
        [],
        [],
        $the_headers,
        $the_json
    );
    $this->assertResponseOk();
    $this->assertEquals(true, $this->response->status);
}

但当然它会返回错误,如:

  • 1)NotesTest :: testPostJson预期状态代码200,得到404。*
  • 2)ErrorException:未定义的属性: 照亮\ HTTP \响应:: $状态

即使在流明文档网站上,文档的方式也不多: https://lumen.laravel.com/docs/5.5/testing

1 个答案:

答案 0 :(得分:0)

这里是工作代码......我错过了$ this-> call()的第二个参数中的“note /”。

> dplyr::arrange(temp3, time, ID)
  ID time A  B
1  1    1 0 99
2  2    1 1 99
3  3    1 0  0
4  1    2 1 99
5  2    2 1 99
6  3    2 0  0

以下是命令行结果:

class NotesTest extends TestCase {

protected $send_headers = null;
protected $send_json = null;
protected $receive_json = null;

public function testIsApiAlive() {
    $this->call("GET",'/');
    $this->assertEquals($this->app->version(), $this->response->getContent());
    $this->assertEquals(200, $this->response->status());
}

public function testPostJson() {
    $this->send_json    = '{"client_guid": "C00A0EA5-3F64-01EA-C4B6-159EA145AB3B"}';
    $this->send_headers = [ 'CONTENT_TYPE' => 'application/json' ];
    $this->call(
        'POST',
        'note/getClientNotes',
        [],
        [],
        [],
        $this->send_headers,
        $this->send_json
    );
    $this->receive_json = json_decode($this->response->getContent());
    $this->assertEquals(200, $this->response->status());
    $this->assertTrue(isset($this->receive_json->status));
    $this->assertTrue($this->receive_json->status == true);
    $this->assertTrue(isset($this->receive_json->data[0]->agency));
    $this->assertTrue($this->receive_json->data[0]->agency == 'demo');
}