Laravel尝试单元测试API JSON响应

Sha*_*ane 11 php phpunit unit-testing laravel

试图在这里一次学习太多新东西(Laravel,PHPUnit等),所以这可能只是一个疲惫的大脑问题,仍然会欣赏一些帮助.

我有一个非常基本的'博客'项目,使用Laravel作为API层,AngularJS作为前端.我想对API端点进行单元测试,但是在我的测试函数中我无法弄清楚如何处理JSON.

当我尝试运行testGetBlogPosts()时,我看到我的CLI中的JSON输出看起来像什么,但我无法json_decode()并检查对象的某些部分是否符合我的预期结果.在这里,我只想确保结果数组中第一个对象的ID是ID"1".

我从测试中得到的结果是:1)ExampleTest :: testGetBlogPosts ErrorException:试图获取非对象的属性

任何帮助或建议非常感谢!

TL; DR:测试用例未正确处理来自API端点的JSON响应

调节器

class HomeController extends BaseController {

    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'HomeController@showWelcome');
    |
    */
    public function showWelcome()
    {
        return View::make('hello');
    }

    public function getBlogPosts()
    {
        $posts = Post::get()->take(5)->toJson();
        // echo $posts; PER THE ACCEPTED ANSWER, RETURN NOT ECHO
        return $posts;
    }
    public function getSinglePost($postId)
    {
        $posts = Post::find($postId)->toJson();
        // echo $posts; PER THE ACCEPTED ANSWER, RETURN NOT ECHO
        return $posts;
    }

}
Run Code Online (Sandbox Code Playgroud)

测试文件

class ExampleTest extends TestCase {

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $crawler = $this->client->request('GET', '/');
        $this->assertTrue($this->client->getResponse()->isOk());
    }

    public function testGetBlogPosts()
    {
        $response = $this->call('GET', 'api/getBlogPosts');

        $array = json_decode($response);
        $result = false;
        if($array[0]->id == 1)
        {
            $result = true;
        }
        $this->assertEquals(true, $result);
    }
}
Run Code Online (Sandbox Code Playgroud)

根据要求,完整的测试输出

root @ homestead:/ home/vagrant/Laravel/Homestead/Blog#phpunit PHPUnit 3.7.28作者Sebastian Bergmann.

配置从/home/vagrant/Laravel/Homestead/Blog/phpunit.xml中读取

.E [{"id":"1","user_id":"1","title":"这是一个测试帖子","post_body":"testststs","created_at":"2014-08-07 19:26:26","updated_at":"2014-08-07 19:26:26"},{"id":"2","user_id":"75","title":"Libero rerum rem praesentium et et at doloribus asperiores.","post_body":"Commodi aut beatae aut veritatis eum soluta sint.in aut cumque iure quis.","created_at":"2014-08-07 19:26:26","updated_at ":"2014-08-07 19:26:26"}]

时间:1.85秒,记忆:18.50Mb

有1个错误:

1)ExampleTest :: testGetBlogPosts ErrorException:尝试获取非对象的属性

/home/vagrant/Laravel/Homestead/Blog/app/tests/ExampleTest.php:22

FAILURES!测试:2,断言:1,错误:1.

如果我在浏览器中访问此端点,我会得到这个

[
{
id: 1,
user_id: 1,
title: "This is a test post",
subtitle: "",
post_body: "testststs",
created_at: "2014-08-07 19:26:04",
updated_at: "2014-08-07 19:26:04"
},
{
id: 2,
user_id: 18,
title: "Rem deserunt dolor odit tempore qui eaque labore.",
subtitle: "",
post_body: "Ea a adipisci molestiae vel dignissimos. Ea blanditiis et est.",
created_at: "2014-08-07 19:26:04",
updated_at: "2014-08-07 19:26:04"
}
]
Run Code Online (Sandbox Code Playgroud)

小智 9

希望你现在知道它,但这是我使用的:

$array = json_decode($response->getContent());

  • 我特意使用`$ this-> response-> getContent()`. (2认同)

Don*_*nic 4

getBlogPosts()控制器中的方法会回显而$post不是返回它。这意味着$response您的测试中不会有任何内容json_decode